chapter 5. functions
DESCRIPTION
Chapter 5. Functions. Weiqi Luo ( 骆伟祺 ) School of Software Sun Yat-Sen University Email : [email protected] Office : A309. Chapter five: Functions. 5.1. Functions 5.2. Functions for Computer Science 5.3. Growth of Functions 5.4. Permutation Functions. 5.1. Functions. - PowerPoint PPT PresentationTRANSCRIPT
Chapter 5. Functions
Weiqi Luo (骆伟祺 )School of Software
Sun Yat-Sen UniversityEmail : [email protected] Office : A309
School of Software
5.1. Functions 5.2. Functions for Computer Science 5.3. Growth of Functions 5.4. Permutation Functions
2
Chapter five: Functions
School of Software
Function (mapping, transformation) Let A and B be nonempty sets. A function f from A to B,
which is denoted f: AB, is a relation from A to B such that for all a in Dom(f), f(a), the f-relative set of a, contains just one element of B.
5.1. Functions
3
A B
ab=f(a)
f a: an argument of f
f(a): the value of f for the argument a or the image of a under f
School of Software
Example 1 Let A={1, 2, 3, 4} and B={a, b, c, d}, and let
f={(1,a), (2,a), (3,d), (4,c)}
Then we have
f(1)=a, f(2)=a, f(3)=d, f(4)=c
since each set f(n) is a single value, f is a function.
Note : a in B appears as the second element of two different ordered pairs in f. This does not conflict with the definition of a function.
5.1 Functions
4
School of Software
Example 2 Let A={1,2,3} and B={x, y, z}. Consider the relations
R={(1,x),(2,x)} and S={(1,x),(1,y),(2,z),(3,y)}
The relation S is not a function since S(1)={x, y}.
The relation R is a function, and Dom(R)={1,2}, and Ran(R)={x}
5.1 Functions
5
School of Software
Example 3 Let P be a computer program that accepts an integer as input
and produces an integer as output. Let A=B=Z. Then P determines a relation fp defined as follow:
(m, n) in fp means that n is the output produced by program P where the input is m.
It is clear that fp is a function, since any particular input corresponds to a unique output (we assume that the computer results are reproducible, that is, they are the same each time the program is run)
Note: Functions can be regarded as “input-output” relations
5.1 Functions
6
School of Software
Example 5 Labeled digraph is a digraph in which the vertices or the
edges (or both) are labeled with information from a set.
V: vertices , E: edges , L: labels
f: VL
g: EL
e.g.
5.1 Functions
7
Boston
Worcester
Providence
New Haven
Hartford
4449
106
64
39
51
School of Software
Example 7 Let A=Z and B={0,1}. Let f: AB be found by
Example 8
Let A be an arbitrary nonempty set. The identity function on A, denoted by 1A, is defined by
1A(a)=a
5.1 Functions
8
1 ( )
0
if a is evenf a
if a is odd
School of Software
Composition Let f: AB and g: BC are functions. Then the
composition of f and g, gof, is a relation from A to C.
5.1 Functions
9
gof
A
f
ab=f(a)
B
C
g
c=f(b)= gof (a)
School of Software
Example 9 Let A=B=Z, and C be the set of even integers.
Let f: AB, and g: BC be defined by
f(a)= a +1 f(b)= 2b
Find gof
Solution: we have
(gof)(a)
= g(f(a))
= g(a+1)
=2(a+1)
5.1 Functions
10
School of Software
Let f be a function from A to B Everywhere defined : Dom(f) = A
Onto : Ran(f) = B
One to one : If f(a) = f(a’) then a=a’
Bijection :
Dom(f) one to one Ran(f)
One-to-one correspondence between A and B
Dom(f) one to one Ran(f)
& Dom(f)=A and Ran(f)=B
5.1 Functions
11
School of Software
Example 11 Let A=B=Z and let f: AB be defined by
f (a) = a+1 for all a in A
Everywhere defined? Yes, Dom(f) =A=Z
Onto? Yes, for all b in B, we have a=b-1
One to one? Yes, a+1 = a’+1, then we have a=a’
5.1 Functions
12
School of Software
Invertible A function f: AB is said to be invertible if its inverse
relation, f-1, is also a function.
Example 14
Let f = {(1,a), (2,a), (3,d), (4,c)}
Then f-1 = {(a,1), (a,2), (d,3), (c,4)}
We see that f-1 is not a function, since f-1 (a)={1,2}
5.1 Functions
13
School of Software
Theorem 1 Let f: AB be a function
(a) Then f-1 is a function from B to A if and only if f is one to one.
If f-1 is a function, then
(b) the function f-1 is also one to one
(c) f-1 is everywhere defined if and only if f is onto
(d) f-1 is onto if and only if f is everywhere defined
5.1 Functions
14
School of Software
Example 15
Let A=B=Z and let f: AB be defined by
f(a) = a + 1 for all a in A
Since it is everywhere defined, one to one, and onto, f is a one-to-one correspondence between A and B.
Thus f is invertible, and f-1 is a one-to-one correspondence between B and A.
5.1 Functions
15
School of Software
Theorem 2Let f: AB be any function. Then
(a) 1B o f = f
(b) f o 1A=f
If f is a one-to-one correspondence between A and B, then
(c) f-1 o f = 1 A
(d) f o f-1 =1 B
5.1 Functions
16
School of Software
Theorem 3 (a) Let f :AB and g: BA be functions such that
g o f = 1A and f o g = 1 B.
Then f is a one-to-one correspondence between A and B, g is a one-to-one correspondence between B and A, and each is the inverse of the other.
(b) Let f : AB and g: BC be invertible. The g o f is invertible, and (g o f) -1 =f-1 o g-1
5.1 Functions
17
School of Software
Theorem 4 Let A and B be two finite sets with the same number of
elements, and let f: AB be an everywhere defined function
(a) If f is one to one, then f is onto
(b) If f is onto, then f is one to one
(Refer to Section 3.3. Pigeonhole Principle)
5.1 Functions
18
School of Software
Example 18
CKAJADPDGKJYEIDOT
JOHNHASALONGBEARD
5.1 Functions
19
School of Software
Homework
Ex. 2, Ex. 10, Ex. 20, Ex. 32, Ex. 36, Ex.41
5.1 Functions
20
School of Software
The characteristic function of A Let A be a subset of the universal set U={u1,u2,…un}. The
characteristic function of A is defined as a function from U to {0,1} by the following:
e.g. If A={4,7,9} and U={1,2,…,10} , then
fA(2)=0, fA(4)=1, fA(7)=1, fA(12) is undefined.
It is everywhere defined and onto, but is not one to one
5.2 Functions for Computer Science
21
1 ( )
0 i
A ii
if u Af u
if u A
School of Software
Boolean function A function from a set A to B, where B ={True, False} is
called a Boolean function.
Note: the predicates in Section 2.1 are examples of Boolean functions.
For instance:
Let P(x): x is even and Q(y): y is odd. Then P and Q are Boolean functions from Z to B.
R(x, y): x is even or y is odd is a Boolean function of two variables from Z×Z to B
5.2 Functions for Computer Science
22
School of Software
Floor Function
the largest integer less than or equal to q
where q is rational numbers
Ceiling Function
the smallest integer greater than or equal to q
where q is rational numbers
5.2 Functions for Computer Science
23
( )f q q
( )f q q
(1.5) 1.5 1, ( 3) 3 3, ( 2.7) 2.7 3f f f
(1.5) 1.5 2, ( 3) 3 3, ( 2.7) 2.7 2f f f
School of Software
Polynomial function
Exponential function
Logarithm function
Factorial function
5.2 Functions for Computer Science
24
20 1 2( ) ... n
np x a a x a x a x
( ) 2zf z
( ) log ( )n nf x x
( ) !f n n
School of Software
Example 7 (a) l: A*Z as l(w) is the length of the string w
(b) B is a finite subset of U
pow(B) denotes the power set of B
Note: pow is a function from V, the power set of U, to the power set of V
(c) Let A=B=the set of all 2-by-2 martices with entries and let t(M)=MT, the transpose of M
5.2 Functions for Computer Science
25
School of Software
Hash Functions Suppose that 10,000 customer account records must be stored
and processed. The company’s computer is capable of searching a list of 100 items in an acceptable amount of time. We decide to create 101 liked lists for storage, because if the hashing function works well in randomly assigning records to lists, we would expect to see roughly 100 records per list. We define a hashing function from the set of set of seven-digit account numbers to the set {0,1,2,3,…,100} as follows
h(n) = n (mod 101) h(2011) = 92
the record with account number 2011 will be assigned to 92.
5.2 Functions for Computer Science
26
School of Software
Collision Since the function in Ex. 10 is not one-to-one, different
accounts may be assigned to the same list. In such a case, the collision occurred.
5.2 Functions for Computer Science
27
List #0
List #1
List #92 :2011193 1001…
List #100
…
…
customer account records
2011
193
1001
…Around 100 per list
School of Software
Fuzzy set and Fuzzy logic
Refer to
http://en.wikipedia.org/wiki/Fuzzy_mathematics
5.2 Functions for Computer Science
28
School of Software
Homework
Ex. 4, Ex. 7, Ex. 8, Ex.18, Ex. 22, Ex. 24
5.2 Functions for Computer Science
29
School of Software
Let f and g be functions whose domains are subsets of Z+. We say that f is O(g), read f is big-Oh of g, if there exist constants c and k such that |f(n)|<=c |g(n)| for all n >=k.
If f is O(g), then f grows no faster than g does.
5.3 Growth of Functions
30
| ( ) |lim ,
| ( ) |n
f nc c R
g n
School of Software
Example 2
The function f(n) = ½ n3 + ½ n2 is O(g) for g(n)=n3. To see this, consider
Choosing 1 for c and 1 for k, we have shown that |f(n)| <=c |g(n)| for all n>=1 and f is O(g).
5.3 Growth of Functions
31
3 2 3 3 31 1 1 1, 1
2 2 2 2n n n n n if n
School of Software
Let f and g be functions whose domains are subsets of Z+. We say that f and g have the same order if f is O(g) and g is O(f).
f is O(g) : f grows no faster than g does
g is O(f) : g grows no faster than f does
5.3 Growth of Functions
32
2 1 1 2
| ( ) |lim ,
| ( ) |n
f nc c c c R
g n
School of Software
Example 3
Let f(n) = 3n4-5n2 and g(n) = n4 be defined for positive integers n. Then f and g have the same order. First,
Let c=8 and k=1, then |f(n)| <=c |g(n)| for all n>=k. Thus f is O(g). Conversely,
Let c=1 and k=2, we have g is O(f).
5.3 Growth of Functions
33
4 2 4 2 4 4 43 5 3 5 3 5 8 , 1n n n n n n n if n
4 4 4 4 23 2 3 5 , 2n n n n n if n
School of Software
Let f and g be functions whose domains are subsets of Z+. We say that f is lower order than g or that f grows more slowly than g if
f is O(g) but g is not O(f)
5.3 Growth of Functions
34
| ( ) |lim 0
| ( ) |n
f n
g n
School of Software
Example 4
f(n) = n5, g(n) = n7
Clearly,
f(n) is O(g), since n5<=n7 , n>=1
Support g(n) is O(f), then there exists c and k such that
n7 <=cn5 , for n>=k
Choose N so that N>k and N2>c, then
N7 <=cN5 < N2 N5= N7
This is a contradiction.
5.3 Growth of Functions
35
School of Software
We define a relation Θ, big-theta, on functions whose domain are subsets of Z+ as f Θ g if and only if f and g have the same order.
Theorem 1: The relation Θ is an equivalence relation
Proof:
Reflexive: |f(n)| <= c |f(n)| , c=1 for all n>=1
Symmetric: by the definition of the same order
(g and f have the same order iff f is O(g) and g is O(f) )
5.3 Growth of Functions
36
School of Software
Transitive:
support f and g have the same order
|f(n)| <=c1|g(n)| for all n>=k1 (1)
|g(n)| <=c2|f(n)| for all n>=k2 (2)
support g and h have the same order
|g(n)| <=c3|h(n)| for all n>=k3 (3)
|h(n)| <=c4|g(n)| for all n>=k4 (4)
|f(n)| <= c1|g(n)| <=c1c3|h(n)| for all n>=max(k1,k3)
|h(n)| <=c4|g(n)| <=c2c4|f(n)| for all n>= max(k2,k4)
Thus, f and h has the same order
5.3 Growth of Functions
37
School of Software
Example 5 All functions that have the same order as g(n)=n3 are
said to have order Θ(n3).
The common orders in computer science applications are
Θ(1), Θ(lg(n)), Θ(n), Θ(nlg(n)), Θ(n2), Θ(n3), and Θ(2n)
Note:
Θ(1): the classes of constant functions
lg is the base 2 log function
5.3 Growth of Functions
38
School of Software
Example 6 Every logarthmic function f(n)=logb(n) has the same order as
g(n)=lg(n). There is a logarithmic change-of-base identity
in which loga(b) is a constant. Thus
and, conversely,
Therefore g is O(f) and f is O(g).
5.3 Growth of Functions
39
log ( )log ( )
log ( )a
ba
xx
b
1| log ( ) | | lg( ) |," "
| lg( ) |b x n holdsb
| lg( ) | lg( ) | log ( ) |," "bn b n holds
School of Software
Rules for determining the Θ-Class of a Function
1. Θ(1) functions are constant and have zero growth, the slowest growth possible.
2. Θ(lg(n)) is lower than Θ(nk) if k>0
3. Θ(na) is lower than Θ(nb) iff b>a>0
4. Θ(an) is lower than Θ(bn) iff b>a>0
5. Θ(nk) is lower than Θ(an) for any power nk and any a>1
6. If r is a not zero, and Θ(rf)= Θ(f) for any function f
7. If h is a nonzero function and Θ(f) is lower than (or the same as) Θ(g), then Θ(fh) is lower than (or the same as Θ(gh) )
8. If Θ(f) is lower than Θ(g), then Θ(f+g) = Θ(g)
5.3 Growth of Functions
40
School of Software
Example 7
Determine the Θ-class of each of the following
(a)f(n) = 4n4-6n7+24n3
(b)g(n) = lg(n) -3n
(c)h(n) = 1.1n+n15
(a) Θ(f) = Θ(n7) (Rules 3,6 and 8)
(b) Θ(g)= Θ(n) (Rules 2,6 and 8)
(c) Θ(h)= Θ(1.1n) (Rules 5 and 8)
5.3 Growth of Functions
41
School of Software
Example 8 Using the rules for ordering Θ-classes, arrange the
following in order from lowest to highest
Θ(nlg(n)) Θ(100n2-n) Θ(n0.2) Θ(1,000,000)
Θ(1.3n) Θ(n+107)
Θ(1,000,000) , Θ(n0.2), Θ(n+107) , Θ(nlg(n)),
Θ(100n2-n) Θ(1.3n)
Θ(n1.2) ?
5.3 Growth of Functions
42
School of Software
Homework
Ex. 6, Ex. 10, Ex. 12, Ex. 25, Ex. 29
5.3 Growth of Functions
43
School of Software
Permutation A bijection from a set A to itself is called a
permutation of A.
For instance,
Let A=R and let f: AA be defined by f(a)=2a+1.
Since f is one to one and onto, it follows that f is a permutation of A.
5.4 Permutation Functions
44
School of Software
If A ={a1,a2,…an} is a finite set and p is a bijection on A, we list the elements of A and the corresponding function values p(a1), p(a2), …, p(an) in the following form:
P is a permutation of a finite set A= {a1,a2,…an} , then the sequence p(a1) , p(a2), …, p(an) is just a rearrangement of the elements of A.
5.4 Permutation Functions
45
1 2
1 2
, , ...,
( ), ( ), ..., ( )n
n
a a ap
p a p a p a
School of Software
Example 2 Let A={1,2,3}. Then all the permutations of A are
5.4 Permutation Functions
46
1 2 31
1 2 3A
1
1 2 3
1 3 2p
2
1 2 3
2 1 3p
3
1 2 3
2 3 1p
4
1 2 3
3 1 2p
5
1 2 3
3 2 1p
School of Software
Example 3 Using the permutations of Example 2, compute (a) p4
-1 (b) p3
o p2
(a) Viewing p4 as a function, we have
P4={(1,3), (2,1), (3,2)}
then P4-1 ={(3,1), (1,2), (2,3)}
5.4 Permutation Functions
47
4
1 2 3
3 1 2p
14 3
1 2 3
2 3 1p p
School of Software
(b)
5.4 Permutation Functions
48
3 2
1 2 3 1 2 3
2 3 1 2 1 3p p
1 2 3
3 2 1
School of Software
Theorem 1 If A={a1,a2,…,an} is a set containing n elements, then
there are n!=n× (n-1) × …2 × 1 permutations of A
p(a1) --- n ways
p(a2) --- n-1 ways (p(a1) is fixed)
…
p(an-1) --- 2 ways (p(a1),…p(an-2) are fixed)
p(an) --- 1 way (p(a1),…p(an-1) are fixed)
Totally, there are n! permutations of A.
5.4 Permutation Functions
49
School of Software
Cyclic Permutation Let b1, b2, …, br be distinct elements of the set A={a1,a2,…,an}. The
permutation p: AA defined by
p(b1)= b2
p(b2)= b3
…
p(br-1)= br
p(br) = b1
p(x)=x, if x in A, x is not in {b1,b2,…,br}
is called a cyclic permutation of length r, or simply a cycle of length r, denoted by (b1,b2,…,br)
5.4 Permutation Functions
50
br-1
br
b1
b2
b3
School of Software
Example 4
Let A={1,2,3,4,5}.The cycle (1,3,5) denotes the permutation
5.4 Permutation Functions
51
1 2 3 4 5
3 2 5 4 1
1
35
Note: (1, 3, 5) = (3, 5, 1) = ( 5,1, 3)
School of Software
Example 5
Let A={1,2,3,4,5,6}. Compute (4,1,3,5) o (5,6,3) and (5,6,3) o (4,1,3,5)
Solutions:
5.4 Permutation Functions
52
1 2 3 4 5 6(4,1,3,5)
3 2 5 1 4 6
1 2 3 4 5 6(5,6,3)
1 2 5 4 6 3
School of Software
Example 5
5.4 Permutation Functions
53
1 2 3 4 5 6(4,1,3,5) (5,6,3)
3 2 4 1 6 5
1 2 3 4 5 6(5,6,3) (4,1,3,5)
5 2 6 1 4 3
(4,1,3,5) (5,6,3) (5,6,3) (4,1,3,5)
Neither product is a cycle. (why?)
School of Software
Two cycles of a set A are said to be disjoint if no element of A appears in both cycles.
For instance,
Let A={1,2,3,4,5,6}.
Then the cycles (1,2,5) and (3,4,6) are disjoint,
whereas the cycles (1,2,5) and (2,4,6) are not.
5.4 Permutation Functions
54
School of Software
Theorem 2 A permutation of a finite set that is not the identity or a
cycle can be written as a product of disjoint cycles of length >=2
For instance:
5.4 Permutation Functions
55
12345678
34652187p
(7,8) (2,4,5) (1,3,6)
(2, 4,5) (1,3,6) (7,8) ...
p
Note: the product is unique except for the order of the cycles
School of Software
Transposition A cycle of length 2 is called a transposition.
p(ai)=aj ; p(aj)=ai
Note:
p o p = 1A - the identity permutation of A
Every cycle can be written as a product of transpositions.
(b1, b2, …, br) = (b1,br) o (b1,br - 1)…(b1,b3) o (b1,b2)
(not unique! Why?)
The proof is shown as below
5.4 Permutation Functions
56
School of Software
Basic Step
r=2, then the cycle is (b1,b2) , which has the proper form
Induction Step
Use P(k) to show P(k+1)
(b1,b2,…,bk,bk+1) be cycle of length k+1
(b1,b2,…,bk,bk+1) = (b1,bk+1) o (b1,b2,…,bk) (why?)
Using P(k),
(b1,b2,…,bk) = (b1,bk) o (b1,bk-1) … (b1,b2)
Thus, by substitution
(b1,b2,…,bk, bk+1) = (b1, bk+1) o (b1,bk) o (b1,bk-1)… (b1,b2)
5.4 Permutation Functions
57
School of Software
Corollary 1
Every permutation of a finite set with at least two elements can be written as a product of transpositions
5.4 Permutation Functions
58
School of Software
Example 9 Write the following permutation as a product of
transposition
5.4 Permutation Functions
59
12345678
34652187p
(7,8) (2,4,5) (1,3,6)p
(2, 4,5) (2,5) (2,4)
(1,3,6) (1,6) (1,3)
(7,8) (2,5) (2,4) (1,6) (1,3)p
School of Software
Every permutation on a set of two or more elements can be written as a product of transpositions in many ways
For instance
5.4 Permutation Functions
60
(1,2,3) (1,3) (1,2)
(2,1) (2,3)
(1,3) (3,1) (1,3) (1,2) (3,2) (2,3)
Even Transpositions
Even Transpositions
Even Transpositions
School of Software
Theorem 3 If a permutation of a finite set can be written as a
product of an even number of transpositions, then it can never be written as a product of an odd number of transpositions, and conversely
Note: a permutation of a finite set is called even if it can be written as a product of an even number of transpositions, and it is called odd if it can be written as a product of an odd number of transpositions.
5.4 Permutation Functions
61
School of Software
Example 10 Is the permutation
even or odd?
5.4 Permutation Functions
62
1234567
2457631p
(3,5,6) (1,2,4,7)p
(1,2,4,7) (1,7) (1,4) (1,2) (3,5,6) (3,6) (3,5)
(3,6) (3,5) (1,7) (1,4) (1,2)p Odd Transpositions
School of Software
From the definition of even and odd permutations, it follows (see Ex. 22-24) that
(a) the product of two even permutations is even
(b) the product of two odd permutations is even
(c) the product of an even and an odd permutation is odd
5.4 Permutation Functions
63
School of Software
Theorem 4 Let A={a1,a2,…,an} be a finite set with n elements, n>=2.
There are n!/2 even permutations and n!/2 odd permutations.
Proof:
An be the set of all even permutations of A
Bn be the set of all odd permutations of A.
We shall define a function f: An Bn, which we show is one to one and onto, and this will show that An and Bn have the same number of elements.
5.4 Permutation Functions
64
School of Software
Since n>=2, we can choose a particular transposition q0 of A. say that q0=(an-1,an). Define f: AnBn by
f(p) = q0 o p p in An
note: p is even permutation, then f(p) is odd and thus f(p) in Bn.
one to one : support p1 and p2 are in An and f(p1)=f(p2)
q0 o p1 = q0 o q2 q0 o (q0 o p1) = q0 o( q0 o q2 )
by associative property
q0 o (q0 o p1) = (q0 o q0 )o p1 = p1 (q0 oq0)=1A
q0 o (q0 o p2) = (q0 o q0 )o p2 = p2 (q0 oq0)=1A
Therefore, p1=p2
5.4 Permutation Functions
65
School of Software
Onto: let q in Bn, then q0oq in An, and
f(q0 o q) = q0 o (q0 o q) = (q0 o q0) o q = q
Therefore, f is an onto function.
Since f is one to one and onto, then An and Bn have the same number of elements. Note that An∩ Bn = ф and by Theorem 1 |An U Bn|= n!, we then have
n! = |An U Bn| = |An| + |Bn| - | An ∩ Bn |= 2 |An|
so
|An|=|Bn|= n!/2
5.4 Permutation Functions
66
School of Software
Homework
Ex. 2, Ex. 6, Ex.24, Ex. 26, Ex. 27, Ex. 37
5.4 Permutation Functions
67