Discrete Mathematics for
Computer Science
Department of Computer Science
Instructor: Dr. Ayesha Altaf
Reference Book: Discrete Mathematics and its applications
BY Kenneth H. Rosen – 8th edition
07/06/2025
1
.
Lecture 10
Chapter 2. Basic Structures
2.3 Functions
2
2.3 Functions
.
From calculus, you are familiar with the
concept of a real-valued function f,
which assigns to each number xR a
value
y = f(x), where yR.
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.)
07/06/2025
3
Function: Formal Definition
.
For any sets A and B, we say that a function
(or “mapping”) f from A to B (f : A B) is a
particular assignment of exactly one element
f(x)B to each element xA.
Functions can be represented graphically in
several ways:
f A B
• • y
a• •b •
•
•
f •
•
A •
B • x
Like Venn diagrams Bipartite Graph Plot
07/06/2025
4
Some Function
Terminology .
If it is written that f : A B, and f(a) = b
(where aA and bB), then we say:
A is the domain of f
B is the codomain of f
b is the image of a under f
a can not have more than 1 image
a is a pre-image of b under f
b may have more than 1 pre-image
The range RB of f is R = {b | a f(a) =
07/06/2025 b }
5
Range versus 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.
07/06/2025
6
Range vs. Codomain:
Example
Suppose I declare that: “f is a function
mapping students in this class to the set of
grades {A, B, C, D, F}.”
At this point, you know f ’s codomain is:
_{A,_B, C, D_,_F_}_, and its range is _unknown!
Suppose the grades turn out all As and Bs.
Then the range of f is _{A,_B} , but its
codomain is _still {A,_B, C, D_, _F_}_!
07/06/2025
7
07/06/2025
8
Function Operators
.
, × (“plus”,“times”) are binary operators
over R. (Normal addition &
multiplication.)
Therefore, we can also add and multiply
two
real-valued functions f,g: R R:
(f g): R R, where (f g)(x) = f(x)
g(x)
(fg): R R, where (fg)(x) = f(x)g(x)
Example 6:
Let f and g be functions from R to R
07/06/2025
such that f(x) = x2 and g(x) = x – x2. 9
Function Composition Operator
Note the match here. It’s necessary!
For functions g: A B and f : B C, there is a
special operator called compose (“◦”).
It composes (creates) a new function from f and
g by applying f to the result of applying g.
We say (f ◦ g): A C, where (f ◦ g)(a) = f(g(a)).
Note: f ◦ g cannot be defined unless range of g
is a subset of the domain of f.
Note g(a)B, so f(g(a)) is defined and C.
Generally, f ◦ g g ◦ f.
07/06/2025
10
Function Composition .
Illustration
g: A B, f : B C
07/06/2025
11
Function Composition: .
Example
g: A B, f : B C
07/06/2025
12
Function Composition: .
Example
Example 20: Let g: {a, b, c} {a, b, c} such that
g(a) = b, g(b) = c, g(c) = a.
Let f : {a, b, c} {1, 2, 3} such that
f(a) = 3, f(b) = 2, f(c) = 1.
What is the composition of f and g, and what
is the composition of g and f ?
f◦g: {a, b, c} {1, 2, 3} such that
(f◦g)(a) = 2, (f◦g)(b) = 1, (f◦g)(c) =
3. (f◦g)(a) = f(g(a)) = f(b) =
2
(f◦g)(b) = f(g(b)) = f(c) = 1
g◦f is not(f◦g)(c)
defined= (why?)
f(g(c)) = f(a) = 3
07/06/2025
13
Function Composition: .
Example
If f(x) = x2 and g(x) = 2x + 1, then what is the
composition of f and g, and what is the
composition of g and f ?
(f◦g)(x) = f(g(x))
= f(2x+1)
= (2x+1)2
(g◦f)(x) = g(f(x))
= g(x2)
= 2x2 + 1
Note that f◦g g◦f. (4x2+4x+1 2x2+1)
07/06/2025
14
Images of Sets under Functions
Given f : A B, and SA,
The image of S under f is simply the set of all
images (under f) of the elements of S.
f(S) = {f(t) | tS}
= {b | tS: f(t) = b}.
Note the range of f can be defined as simply
the image (under f) of f ’s domain.
Let A = {a, b, c, d, e} and B = {1, 2, 3, 4} with f (a) =
2, f (b) = 1, f (c) = 4, f (d) = 1, and f (e) = 1. The image
of the subset S = {b, c, d} is the set f (S) = {1, 4}.
07/06/2025
15
One-to-One Functions
.
A function f is one-to-one (1–1), or injective,
or an injection, iff f(a) = f(b) implies that a = b
for all a and b in the domain of f (i.e. every
element of its range has only 1 pre-image).
Formally, given f : AB,
“f is injective”: a,b (f(a) f(b) a = b) or
equivalently a,b (a b f(a) f(b))
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?
07/06/2025
16
One-to-One Illustration
.
Bipartite (2-part) graph representations of
functions that are (or not) one-to-one:
• • • • • •
• • • •
• •
• • •
• • •
• • • • •
•
• • •
One-to-one Not one-to-one Not even a function!
Example 8:
Is the function f : {a, b, c, d} {1, 2, 3, 4, 5} with
f(a) = 4, f(b) = 5, f(c) = 1, and f(d) = 3 one-to-one?
Example 9:
Let f : Z Z such that f(x) = x2. Is f one-to-one? NO
07/06/2025
17
Sufficient Conditions for .
1–1ness
For functions f over numbers, we say:
f is strictly (or monotonically) increasing
iff x y f(x) f(y) for all x, y in domain;
f is strictly (or monotonically) decreasing
iff x y f(x) f(y) for all x, y in domain;
If f is either strictly increasing or strictly
decreasing, then f is one-to-one.
E.g. x3
07/06/2025
18
Onto (Surjective) Functions
.
A function f : A B is onto or surjective or a
surjection iff for every element bB there is
an element aA with f(a) = b (bB, aA: f
(a) = b) (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.
07/06/2025
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?
07/06/2025
20
Bijections and Inverse Function
A function f 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 f : A B be a bijection.
The inverse function of f is the function that
assigns to an element bB the unique element
aA such that f(a) = b.
The inverse function of f is denoted by f -1: B A.
Hence, f -1(b) = a when f(a) = b.
07/06/2025
21
Inverse Function Illustration
.
Let f: A B be a bijection
Example 16: Let f : {a, b, c} {1, 2, 3} such that
f(a) = 2, f(b) = 3, f(c) = 1. Is f invertible, and if it is,
what is its inverse? Yes. f–1(1) = c, f–1(2) = a, f–1(3) =
b
f(x) = x 2. Is f invertible?
No. f is not
Example 18: Let f be the function fromaRone-to-one
to R with
07/06/2025
function. So it’s not invertible.
22
The Identity Function
.
For any domain A, the identity function
I: A A (also written as IA, 1, 1A) is the
unique function such that aA: I(a) = a.
Note that the identity function is always both
one-to-one and onto (i.e., bijective).
For a bijection f : A B and its inverse
function f -1: B A,
f 1 f A
I
Some identity functions you’ve seen:
0, 1, T, F, , U.
07/06/2025
23
Identity Function Illustrations .
The identity function:
•
• •
• • y y = I(x) = x
• •
• •
Domain and range x
07/06/2025
24
Graphs of Functions
.
We can represent a function f : A B as a set of
ordered pairs {(a, f(a)) | aA}. ← The function’s graph.
Note that aA, there is only 1 pair (a, b).
Later (ch.9): relations loosen this restriction.
For functions over numbers, we can represent
an ordered pair (x, y) as a point on a plane.
A function is then drawn as a curve (set of
points), with only one y for each x.
07/06/2025
25
Graphs of Functions:
Example s
The graph of f(n) = 2n + 1 The graph of f(x) = x2
from Z to Z from Z to Z
07/06/2025
26
Plots with Floor/Ceiling: .
Example
y = ⎣ x⎦ y = ⎡x
⎤
07/06/2025
27
Applications of Floor/Ceiling: .
Data stored on a computer disk or transmitted
over a data network are usually represented as
a string of bytes. Each byte is made up of 8
bits. How many bytes are required to encode
100 bits of data?
Solution: To determine the number of bytes
needed, we determine the smallest integer that
is at least as large as the quotient when 100 is
divided by 8, the number of bits in a byte.
Consequently, 100/8 = 12.5 = 13 bytes are
required.
07/06/2025
28