0% found this document useful (0 votes)
24 views26 pages

Lecture 7

Uploaded by

kayden3635
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views26 pages

Lecture 7

Uploaded by

kayden3635
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 26

Discrete Structures

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:AB and f:BC, 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):AC, 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:

gof(2) = g (f(2)) = g (3) = 7


gof (3) = g (f(3)) = g (4) = 7
gof(4) = g (f(4)) = g (5) = 11
gof(5) = g (5) = 11.

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

– T(1) =1 …… basis case


– T(n) = T(n-1) + 3, for n >= 2 …… recursive
case
Fibonacci Sequence
• The Fibonacci Sequence is defined by:
• F(1) = 1, F(2) = 1 …… basis case
• F(n) = F(n-2) + F(n-1), for n > 2 …… recursive
case
Recursively Defined
Operations
• Certain operations performed on objects can be
defined recursively
• Examples:
– A recursive definition of multiplication:
• m*1=m
• m * n = m * (n-1) + m, for n >= 2
– A recursive definition of exponentiation:
• a0 = 1
• an = (an-1)*a, for n > 0
– A recursive definition of factorial operation:
• F(0) = 1
• F(n) = n * F(n-1) for n >= 1
Recursively
Defined Algorithms
• If a recurrence relation exists for an operation, then the
algorithm for such a relation can be written either
iteratively or recursively
• Example: factorial, multiplication, etc.
• Factorial: S(1) = 2, S(n) = 2S(n-1) for n >= 2

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

• Solve the recurrence relation:


– F(1) = 1
– F(n) = n*F(n-1) for n >= 2
26

You might also like