You are on page 1of 25

Discrete Structures

Lecture 12: Functions


based on slides by Jan Stelovsky
based on slides by Dr. Baek and Dr. Still
Originals by Dr. M. P. Frank and Dr. J.L. Gross
Provided by McGraw-Hill

Muhammad Adeel Zahid


Department of Computer Science
Government College University
Faisalabad
Functions

• From calculus, you are familiar with the concept of a


real-valued function , which assigns to each number
∈ a value = ( ), where ∈ .

• But, the notion of a function can also be naturally


generalized to the concept of assigning elements of
any set to elements of any set. (Also known as a map.)

2
Function: Formal Definition

• For any sets and , we say that a function (or “mapping”) from
to ( ∶ → ) is a particular assignment of exactly one element
( ) ∈ to each element ∈ .
• Functions can be represented graphically in several ways:

3
Terminology

• If it is written that ∶ → , and ( ) = (where


∈ and ∈ ), then we say:
• is the domain of
• is the codomain of
• is the image of under
• can not have more than 1 image
• is a pre-image of under
• may have more than 1 pre-image
• The range ⊆ of is = { |∃ ( ) = }

4
Range vs Codomain

• The range of a function might not be its whole


codomain
• The codomain is the set that the function is declared
to map all domain values into
• The range is the particular set of values in the
codomain that the function actually maps elements of
the domain to

5
Example

• Suppose I declare that: “ is a function mapping


students in this class to the set of grades
{ , , , , }”
• At this point, you know ’s codomain is:
{ , , , , } and its range is unknown
• Suppose the grades turn out all As and Bs
• Then the range of is { , }, but its codomain is still
{ , , , , }

6
Function and Non-Function

a 1 a 1
Valid Valid
b 2 Function b 2
Function
c 3 c 3

d 4 d 4

a 1 1

Not a valid b 2 Not a valid b 2


function because function because
c does not map to c 3 c maps to more c 3
any value than 1 value
d 4 d 4
7
Function Operators

• + ,× (“plus”,“times”) are binary operators over . (Normal addition &


multiplication.)
• Therefore, we can also add and multiply two real-valued functions
, : →
• ( + ): → , where ( + )( ) = ( ) + ( )
• ( ): → , where ( )( ) = ( ) ( )
• Example:
• Let and be functions from to such that ( ) = and ( ) =

• What are the functions + and ?
• ( + )( ) = ( ) + ( ) = + – =
• ( )( ) = ( ) ( ) = ( )( − ) = −

8
Function Composition Operator

• For functions : → and ∶ → , there is a special operator


called compose (“◦”)
• It composes (creates) a new function from and by applying f to
the result of applying
• We say ( ◦ ): → , where ( ◦ )( ) = ( ( ))
• Note: ◦ cannot be defined unless range of is a subset of the
domain of .
• Note ( ) ∈ , so ( ( )) is defined and ∈
• Note that ◦ is non-commuting. (Like Cartesian ×, but unlike +,∧,∪)
(Generally, ◦ ≠ ◦ )

9
Function Composition

• : → , ∶ →

10
Function Composition

• : → , ∶ →

11
Function Composition Example

• Example: Let : { , , } → • ◦ is not defined (why?):


{ , , } such that
( ◦ )( ) = ( ( ))
• ( ) = , ( ) = , ( ) =
• ( ) = 3, but (3) is not defined
• Let ∶ { , , } → {1, 2, 3} such that
• ( ) = 3, ( ) = 2, ( ) = 1
• ( ) = 2, but (2) is not defined
• ( ) = 1, but (1) is not defined
• What is the composition of and ,
and what is the composition of and • Range of is not subset of
? domain of
• ◦ : { , , } → {1, 2, 3} such that • Range of = {1,2,3}
• ( ◦ )( ) = 2, ( ◦ )( ) = • Domain of = { , , }
1, ( ◦ )( ) = 3
• ( ◦ )( ) = ( ( )) = ( ) = 2
• ( ◦ )( ) = ( ( )) = ( ) = 1
• ( ◦ )( ) = ( ( )) = ( ) = 3

12
Function Composition Example

• If ( ) = and ( ) = 2 + 1, then what is the composition of


and , and what is the composition of and ?
( ◦ )( ) = ( ( ))
= (2 + 1)
= 2 +1

( ◦ )( ) = ( ( ))
= ( )
= 2 + 1

• Note that ◦ ≠ ◦ . (4 +4 +1 ≠ 2 + 1)

13
Images of Sets Under Functions

• Given ∶ → , and ⊆ ,
• The image of under f is simply the set of all images (under ) of the
elements of

( ) = { ( )| ∈ }
= { |∃ ∈ : ( ) = }

• Note the range of can be defined as simply the image (under ) of


’s domain

14
One-to-One Function

• A function is one-to-one (1–1), or injective, or an injection, iff


( ) = ( ) implies that = for all and in the domain of
(i.e. every element of its range has only 1 pre-image)
• Formally, given ∶ → ,
• “ is injective”: ∀ , ( ( ) = ( ) → = ) or equivalently
•∀ , ( ≠ → ( ) ≠ ( ))
• Only one element of the domain is mapped to any given one element
of the range
• Domain & range have the same cardinality
• What about codomain?

15
One-to-One Illustration

• Bipartite (2-part) graph representations of functions that are (or not)


one-to-one:

16
One-to-one Functions

• Example:
• Is the function ∶ { , , , } → {1, 2, 3, 4, 5} with
• ( ) = 4, ( ) = 5, ( ) = 1, and ( ) = 3 one-to-one?
• Yes, it is one-to-one
• No element of the range is assigned more than once to any element of the
domain
• Example:
• Let ∶ → such that ( ) = . Is one-to-one?
• No, it is not because (−2) = (2) = 4 even though 2 ≠ −2
• Generally for this function = (− ) even though ≠ −

17
Sufficient Conditions for one-to-oneness

• For functions over numbers, we say:


• is strictly (or monotonically) increasing iff > → ( ) > ( ) for
all , in domain;
• is strictly (or monotonically) decreasing iff > → ( ) < ( ) for
all , in domain;
• If f is either strictly increasing or strictly decreasing, then f is one-to-one.
• E.g.
• −2 = −8
• −1 = −1
• 0 =0
• 1 =1
• 2 =8

18
Onto (Surjective) Functions

• A function ∶ → is onto or surjective or a surjection iff for


every element ∈ there is an element ∈ with ( ) =
• (∀ ∈ , ∃ ∈ : ( ) = ) (i.e. its range is equal to its codomain)
• Think: An onto function maps the set A onto (over, covering) the
entirety of the set B, not just over a piece of it
• E.g., for domain & codomain R, is onto, whereas isn’t. (Why
not?)
• Because does not map any real number to any negative number hence the
codomain and range are not equal

19
Illustration of Onto

• Some functions that are, or are not, onto their codomains:

• Example13: Is the function f(x) = x + 1 from the set of integers to the


set of integers onto?
• Yes, because for every integer ∈ (codomain), we have ∈ (domain) such
that ( ) =

20
Bijections and Inversible Functions

• A function is said to be a one-to-one correspondence, or a


bijection, or reversible, or invertible, iff it is both one-to-one and
onto
• Let ∶ → be a bijection
• The inverse function of f is the function that assigns to an element
∈ the unique element ∈ such that ( ) =
• The inverse function of is denoted by : →
• Hence, ( ) = when ( ) =

21
Inverse Function Illustration

• Let : → be a bijection

22
Inversible Function Examples

• Example: Let ∶ { , , } → {1, 2, 3} such that ( ) =


2, ( ) = 3, ( ) = 1 Is f invertible, and if it is, what is its
inverse?
• Yes. (1) = , (2) = , (3) =

• Example: Let be the function from to with ( ) = Is f


invertible?
• No. is neither a one-to-one function and nor an onto function
• (−2) = (2)
• So it’s not invertible

23
Mappings in Java

• A discrete function can be represented by a Map interface or


HashMap class in Java programming language
• < , >= ℎ < , >
() ;
• Here, the domain is Integer, the codomain is String
• We can construct such a mapping by putting all pairs { , ( )} into
our map. ( is the key, ( ) is the value.)
• . (2, "Jan");
• ( : ){ . ( . , . ); }
• If we put another pair with the same key, it will overwrite the
previous pair

24
Images, Range, Bijection in Java

• . () returns the image


• it’s a Java Set!
• . () returns the range
• it’s a Java Set!
• Is a map a bijection?
• Iff the cardinalities of the image and range are the same:
• ( . (). () == . (). ()) {
• . . ("map is a bijection”);
•}

25

You might also like