Transcript lecture05a

Discrete Structures
Chapter 5
Relations and Functions
Nurul Amelina Nasharuddin
Multimedia Department
Objectives
On completion of this chapter, student should be able
to:
1. Define a relation and function
2. Determine the type of function (one-to-one,
onto, one-to-one correspondence)
3. Find a composite function
4. Find an inverse function
2
Outline
•
•
•
•
•
•
Cartesian products and relations
Functions: Plain, one-to-one, onto
Function composition and inverse functions
Functions for computer science
Properties of relations
Computer recognition: Zero-one matrices and
directed graphs
• Use in database example
3
Relationship
4
Recall: Cartesian Products
• For sets A, B, the Cartesian product, or cross
product, of A and B is denoted by A × B and equals
{(a, b) | a  A, b  B}
• Elements of A × B are ordered pairs. For (a, b),
(c, d)  A × B , (a, b) = (c, d) if and only if a = c and
b=d
5
Recall: Cartesian Products
Properties:
1. If A, B are finite, it follows from the rule of
product that |A × B| = |A||B|
2. Although we generally will not have A × B = B ×
A, we will have |A×B|=|B×A|
6
Example (1)
Let A = {2, 3, 4}, B = {4, 5}. Then
a) A × B = {(2, 4), (2, 5), (3, 4), (3, 5), (4, 4), (4, 5)}
b) B × A = {(4, 2), (4, 3), (4, 4), (5, 2), (5, 3), (5, 4)}
c) B2 = B × B = {(4, 4), (4, 5), (5, 4), (5, 5)}
d) B3 = B × B × B = {(a, b, c) | a, b, c  B}; for
instance, (4, 5, 5)  B3
7
Example (2)
An experiment E is conducted as follows:
A single dice is rolled and its outcome noted,
and then a coin is flipped and its outcome noted.
Determine a sample space S for E
S1={1, 2, 3, 4, 5, 6} be a sample space dice.
S2= {H, T} be a sample space coin.
Then S = S1 × S2 is a sample space for E.
8
Example (2)
9
Example (3)
At the Wimbledon Tennis Championships, women play at most
three sets in a match
The winner is the first to win two sets. If we let N and E denote
the two players, the tree diagram indicates the six ways in
which this match can be won
For example, the starred line segment (edge) indicates that
player E won the first set
The double starred edge indicates that player N has won the
match by winning the first and third sets
10
Example (3)
11
Relations
Let A = {0,1,2}, B = {1,2,3}. A x B = {(0,1), (0,2),
(0,3), (1,1), (1,2), (1,3), (2,1), (2,2), (2,3)}
Let say an element x in A is related to an element y
in B iff x is less than y. x R y: x is related to y
0 R 1, 0 R 2, 0 R 3, 1 R 2, 1 R 3, 2 R 3
The set of all ordered pair in A x B where elements
are related {(0,1), (0,2), (0,3), (1,2), (1,3), (2,3)}
12
Relations
• For sets A, B, a (binary) relation R from A to B is a subset
of A × B. Any subset of A × A is called a (binary) relation
on A
• Given an ordered pair (a, b) in A x B, x is related to y by R
(x R y) iff (x, y) is in R
• In general, for finite sets A, B with |A| = m and |B|= n,
there are 2mn relations from A to B, including the empty
relation as well as the relation A × B itself
13
Example (1)
Let A = {2, 3, 4}, B = {4, 5}. Then
A × B = {(2, 4), (2, 5), (3, 4), (3, 5), (4, 4), (4, 5)}.
The following are some of the relations from A to B.
i. 
ii.
iii.
iv.
v.
vi.
{(2, 4)}
{(2, 4), (2, 5)}
{(2, 4), (3, 4), (4, 4)}
{(2, 4), (3, 4), (4, 5)}
A×B
Since |A × B| = 6, there are 26 possible relations from
A to Β (for there are 26 possible subsets of A × B )
14
Example (2)
Let A = {1,2}, B = {1,2,3} and define a binary
relation from A to be as follows:
Given any (x,y)  A x B, (x,y)  R  x – y is even
a) State explicitly which ordered pairs are in A x B
and which are in R
b) Is 1 R 3?
c) Is 2 R 3?
d) Is 2 R 2?
15
Example (2)
a) A x B= {(1,1), (1,2), (1,3), (2,1), (2,2), (2,3)} and
R, when x – y is even = {(1,1), (1,3), (2,2)} 
AxB
(1,1)  R because 1 – 1= 0 is even
(1,2)  R because 2 – 1 = 1 is not even
b) Is 1 R 3? Yes
c) Is 2 R 3? No
d) Is 2 R 2? Yes
16
Example (3)
Let B={1,2} and A=P(B) = {,{1},{2},{1,2}}
|A×A| = 4.4 = 16
A×A = {(∅,∅),(∅,{1}),(∅,{2}),(∅,{1,2}),
({1},∅), ({1},{1}), ({1},{2}), ({1},{1,2})
({2},∅),({2},{1}), ({2},{2}), ({2},{1,2})
({1,2},∅),({1,2},{1}),({1,2},{2},
({1,2},{1,2})}
The following is an example of a relation on A:
R = {(∅, ∅), (∅, {1}), (∅, {2}), (∅, {1, 2}), ({1}, {1}),
({1}, {1, 2}), ({2}, {2}), ({2}, {1, 2}), ({1, 2}, {1, 2})}
17
Example (4)
• With A = Z+ (set of positive integers), we may
define a relation R on set A as {(x, y) | x ≤ y}
• This is the familiar “is less than or equal to”
relation for the set of positive integers
• It can be represented graphically as the set of
points, with positive integer components, located
on or above the line y = x in the Euclidean plane,
as partially shown in the figure below
18
(7, 7), (7, 11)  R
(8, 2)  R
(7, 11)  R or 7 R 11 (infix notation)
19
Arrow Diagrams of Relations
• Let A = {1,2,3}, B = {1,3,5}
• For all x A and y B, relations S and T
(x,y)  S  x < y
T = {(2,1), (2,5)}
20
Functions
For nonempty sets A and B,
A function, or mapping, f from A to B, denoted
f: A  B, is a relation from A to B in which every
element of A appears exactly once as the first
component of an ordered pair in the relation
Sample functions:
f : R  R, f(x) = x2
f : Z  Z, f(x) = x + 1
f : Q  Z, f(x) = 2
21
Functions
•
A function f from a non-empty set A to a set B is a relation
from A to B satisfying the following two properties:
1) x  A, y  B such that (x,y)  f
2) (x, y), (x, y’)  f, y = y’
•
•
•
The 1st property says every x  A is related to at least one
yB
The 2nd property says each x  A is related to at most one
yB
That is, a relation from A to B is a function from A to B if and
only if every x  A is related to exactly one y  B
22
Example (1)
Let A = {1,2,3}, B = {7,8,9}
a) g = {(1,8), (2,9), (3,9), (3,10)}  A x B is not a
function from A to B: (3,9), (3,10)  g but 9  10.
Relation g fails to be a function because 3  A is
related to two (distinct) elements 9, 10  B
b) h = {(1,9), (2,10), (3,9)}  A x B is a function
from A to B. Relation h is a function because each
element of A is related to exactly one element in B
23
Arrow Diagram
• We often write f(a) = b when (a, b) is an ordered pair in the
function f. For (a, b)  f, b is called the image of a under f,
whereas a is a preimage (inverse image) of b
24
Arrow Diagram
The arrow diagram of a function from A to B has the
characteristic that there is exactly one arrow shooting out from
every element of A
However, a element of B can be hit by no arrows, one arrow,
or many arrows
25
Domain and Codomain
• For the function f: A → B, A is called the domain of f and B
the codomain of f
• The subset of B consisting of those elements that appear as
second components in the ordered pairs of f is called the
range of f and is also denoted by f (A) because it is the set of
images (of the elements of A) under f
• Eg: Let A = {1, 2, 3}, B = {w, x, y, z }, f={(1, w), (2, x), (3,
x)}
Domain of f = {1,2,3}, the codomain of f = {w, x, y, z}, and
the range of f = f (A) = {w, x}
26
Interesting Functions in Computer Science
Greatest integer function, or floor function:
This function f: R → Z, is given by f(x) =  x  = the greatest
integer n less than or equal to x, n  x  n + 1
Consequently, if x is a real number and n is an integer, then f(x)
=  x  = is the integer to the immediate left of x on the real
number line. For this function, we find that
1)  3.8  = 3,  3  = 3, –3.8  = –4, –3  = –3;
2)  7.1 + 8.2  =  15.3  = 15 = 7 + 8 =  7.1  +  8.2 
3)  7.7 + 8.4  =  16.1  = 16 ≠ 15 = 7 + 8 =  7.7  +  8.4 
27
Interesting Functions in Computer Science
Ceiling function:
This function g: R → Z, is given by g(x) =  x  = the least
integer greater than or equal to x, n  x  n + 1
Consequently, if x is a real number and n is an integer, then g(x)
=  x  = is the integer to the immediate right of x on the real
number line. For this function, we find that
1)  3 = 3,  3.01 =  3.7 = 4 =  4 , –3.01 = –3.7 = –3;
2)  3.6 + 4.5 =  8.1 = 9 = 4 + 5 =  3.6 +  4.5
3)  3.3 + 4.2 =  7.5 = 8 ≠ 9 = 4 + 5 =  3.3 +  4.2
28
Interesting Functions in Computer Science
Trunc function (for truncation):
valued function defined on R. This function deletes the fractional
part of a real number
For example, trunc(3.78) = 3, trunc(5) = 5, trunc(–7.22) = –7
Note that trunc(3.78) = 3.78 = 3 while trunc(–3.78) = –3.78
= –3
29
Total Number of Functions
For general case, let A, B be nonempty sets with |A| = m, |B| =
n. Consequently, If A = {a1, a2, …, am} and B={b1,b2,…,bn},
then a typical function f: A → B can be described by
{(a1, x1), (a2, x2), (a3, x3), …, (am, xm)} – m ordered pairs.
x1 can selected from any of the n elements of B
x2
“
…………………..
xm
“
In this way, using the rule of product, there are nm = |B||A|
functions from A to B
30
Total Number of Functions
• Let A = {1, 2, 3}, B = {w, x, y, z}, f = {(1, w), (2,
x), (3, x)}
• There are 24.3=212 = 4096 relations from A to B
• We have examined one function among these relations, and
now we wish to count the total number of functions from A
to B
• Therefore, there are 43 = |B||A| = 64 functions from A to B,
and 34 = |A||B| = 81 functions from B to A
31
Properties of Functions
• Two important properties that functions may satisfy:
a) The property of being one-to-one and
b) The property of being onto
• Functions that satisfy both properties are called oneto-one correspondences or one-to-one onto
functions
32
One-to-one Function
• Let f be a function from A to B. f is called one-toone, or injective, iff for all elements x1 and x2 in A
If f(x1) = f(x2), then x1 = x2
or, equivalently if x1  x2, then f(x1)  f(x2)
• Each element of B appears at most once as the
image of an element of A
33
One-to-one Function
34
Not One-to-one Function
35
One-to-one Function
• If f: A → B is one-to-one, with A, B finite, we must
have |A|≤|B|
• For arbitrary sets A, B, f: A → B is one-to-one if
and only if for all, a1, a2  A,
f (a1) = f (a2)  a1 = a2
36
Identifying One-to-one Functions
Defined on Finite Sets
• Let X = {1,2,3} and Y = {a,b,c,d}
Define H: X  Y as follows: H(1) = c, H(2) = a,
H(3) = d. Is H one-to-one?
Define K: X  Y as follows: K(1) = d, K(2) = b,
K(3) = d. Is K one-to-one?
37
Identifying One-to-one Functions
Defined on Infinite Sets
Suppose f is a function defined on an infinite set X. By
definition, f is one-to-one iff the following is true:
x1, x2  X, if f(x1) = f(x2), then x1 = x2
(1) Suppose x1 and x2 are elements of X such that
f(x1) = f(x2)
(2) Show that x1 = x2
38
Example (1)
Consider the function f: R→ R where f (x) = 3x + 7
for all x  R
Then for all x1 , x2 ,  R, we find that
f (x1) = f (x2)  3x1 + 7 = 3x2 + 7
 3x1 = 3x2 (minus both side with 7)
 x1 = x2 , (dividing both side with 3)
so the given function f is one-to-one
39
Example (2)
On the other hand, suppose that g: R → R is the function
defined by g (x) = x4 – x for each real number x
Let x1 = 0 and x2 =1.Then
g(x1) = g(0) = (0)4 – 0 = 0
g(x2) = g(1) = (1)4 – (1) = 1 – 1 = 0
Hence g(x1) = g(x2) but x1  x2 (0 ≠ 1) – that is, g is not
one to-one because there exist real numbers x1, x2 where g
(x1) = g (x2) but x1  x2
40
Example (3)
Let A = {1, 2, 3} and B = {1, 2, 3, 4, 5}
The function f = {(1, 1), (2, 3), (3, 4)} is a one-to-one
function from A to B;
g = {(1, 1), (2, 3), (3, 3)} is a function from A to B, but
fails to be one-to-one because g(2) = g(3) = 3 but 2 ≠ 3
For A, B in the above example, there are 215 relations
from A to B and 53 of these are functions from A to B.
The next question we want to answer is how many
functions f: A → B are one-to-one
41
Calculate Total No of One-to-one Functions
• With
A = {a1, a2, a3, …, am},
B = {b1, b2, b3, …, bn}, and m ≤ n ,
a one-to-one function f: A → B has the form
{(a1, x1), (a2, x2), (a3, x3), …, (am, xm)},
• Where there are
n
choices for x1
n–1
choices for x2
n–2
choices for x3
………..
n – m+1 choices for xm.,
• The number of one-to-one functions from A to B is
n(n-1)(n-2)…(n-m+1)= n!/(n-m)! = P(n,m)= P(|B|,|A|)
42
Example (1)
• Consequently, for A, B where A = {1, 2, 3} and
B = {1, 2, 3, 4, 5}, there are
P(5,3)
= P(|B|,|A|)
=5 . 4 . 3
= 60 one-to-one functions f: A → B.
43
Onto Function
• A function f: A→ B is called onto, or surjective, if
f (A) = B – that is, if for all b  B there is at least
one a  A with f (a) = b
44
Not Onto Function
45
Identifying Onto Functions Defined on
Finite Sets
• Let X = {1,2,3,4} and Y = {a,b,c}
Define H: X  Y as follows: H(1) = c, H(2) = a,
H(3) = c, H(4) = b. Is H onto?
Define K: X  Y as follows: K(1) = c, K(2) = b,
K(3) = b, K(4) = c. Is K onto?
46
Identifying Onto Functions Defined
on Infinite Sets
Suppose f is a function from a set X to a set Y, and
suppose Y is infinite. By definition, f is onto iff the
following is true:
y  Y, x  X such that f(x) = y
(1) Suppose that y is any element of Y
(2) Show that there is an element of X with f(x) = y
47
Example (1)
The function f: R → R defined by f(x) = x3 is an onto
function
If r is any real number in the codomain of f, then the
real number 3√r is in the domain of f and
f(3√r) = (3√r)3 = r
E.g. f(3) = 27, f(-3) = -27
Hence the codomain of f = R = range of f, and the
function f is onto
48
Example (2)
• The function g: R → R, where g(x) = x2 for each
real number x, is not an onto function
• In this case, no negative real number appears in the
range of g
• For example, for –9 to be in the range of g, we
would have to be able to find a real number r with
g(r) = r2 = –9
• Note, however, that the function h: R → [ 0, +∞ )
defined by h(x) = x2 is an onto function
49
Example (3)
• Consider the function f: Z → Z, where f(x) = 3x + 1
for each x  Z
• Here the range of f = {…, –8, –5, –2, 1, 4, 7, …} 
Z, so f is not an onto function
• E.g. f(x) = 3x + 1 = 8 then x = 7/3
• Rational number 7/3 is not an integer –so there is no
x in the domain Z with f(x) = 8
50
Example (4)
On the other hand, each of the functions
1) g: Q → Q, where g(x) = 3x + 1 for x  Q; and
2) h: R → R, where h(x) = 3x + 1 for x  R
is an onto function (Q is a set of rational numbers:
a/b)
Furthermore, 3x1 + 1 = 3x2 + 1  3x1= 3x2  x1 = x2,
regardless of whether x1 and x2 are integers, rational
numbers, or real numbers
Consequently, all three of the functions f, g and h are
one-to-one
51
Example (5)
If A = {1, 2, 3, 4} and B = {x, y, z}, then
f1 = {(1, z), (2, y), (3, x), (4, y)} and
f2 = {(1, x), (2, x), (3, y), (4, z)}
are both functions from A onto B
However, the function g = {(1, x), (2, x), (3, y), (4,y)}
is not onto, because g(A) = {x, y}  B (no z!)
If A, B are finite sets, then for an onto function f: A →
B to possibly exist we must have |A| ≥ |B| where
|A|= m ≥ n = |B|
52
One-to-one Correspondences
• If f: A → B, then f is said to be bijective, or to be a
one-to-one correspondences, if f is both one-to-one
and onto.
• Eg: If A = {1, 2, 3, 4} and B = {w, x, y, z}, then
f = {(1,w),(2,x),(3,y),(4, z)} is a one-to-one
correspondence from A (on) to B, Why?
Ans: f is one-to-one (every element of B appear at
most once), and f is onto (f(A) = B)
53
Example (1)
• Let A = {1, 2, 3, 4} and B = {w, x, y, z}, and g =
{(w, 1), (x, 2), (y, 3), (z, 4)}. Is g a one-to-one
correspondence from B (on) to A?
54