VIEWS: 3 PAGES: 67 POSTED ON: 7/19/2012
Chapter 5. Functions Weiqi Luo (骆伟祺) School of Software Sun Yat-Sen University Email：weiqi.luo@yahoo.com Office：A309 Chapter five: Functions 5.1. Functions 5.2. Functions for Computer Science 5.3. Growth of Functions 5.4. Permutation Functions 2 School of Software 5.1. Functions 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. f a: an argument of f f(a): the value of f for the b=f(a) argument a a or the image of a under f A B 3 School of Software 5.1 Functions 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. 4 School of Software 5.1 Functions 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 School of Software 5.1 Functions 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 6 School of Software 5.1 Functions 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 Boston g: EL 44 e.g. Worcester 49 51 64 Providence Hartford 106 39 New Haven 7 School of Software 5.1 Functions Example 7 Let A=Z and B={0,1}. Let f: AB be found by 1 if a is even f (a) 0 if a is odd Example 8 Let A be an arbitrary nonempty set. The identity function on A, denoted by 1A, is defined by 1A(a)=a 8 School of Software 5.1 Functions Composition Let f: AB and g: BC are functions. Then the composition of f and g, gof, is a relation from A to C. g of f g a c=f(b) b=f(a) B = gof (a) A C 9 School of Software 5.1 Functions 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) 10 School of Software 5.1 Functions 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 11 School of Software 5.1 Functions 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’ 12 School of Software 5.1 Functions 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} 13 School of Software 5.1 Functions 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 14 School of Software 5.1 Functions 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. 15 School of Software 5.1 Functions Theorem 2 Let 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 16 School of Software 5.1 Functions 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 17 School of Software 5.1 Functions 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) 18 School of Software 5.1 Functions Example 18 CKAJADPDGKJYEIDOT JOHNHASALONGBEARD 19 School of Software 5.1 Functions Homework Ex. 2, Ex. 10, Ex. 20, Ex. 32, Ex. 36, Ex.41 20 School of Software 5.2 Functions for Computer Science 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: 1 if ui A f A (ui ) 0 if ui A 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 21 School of Software 5.2 Functions for Computer Science 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 22 School of Software 5.2 Functions for Computer Science Floor Function f (q) q the largest integer less than or equal to q where q is rational numbers f (1.5) 1.5 1, f (3) 3 3, f (2.7) 2.7 3 Ceiling Function f (q) q the smallest integer greater than or equal to q where q is rational numbers f (1.5) 1.5 2, f (3) 3 3, f (2.7) 2.7 2 23 School of Software 5.2 Functions for Computer Science Polynomial function p ( x) a0 a1 x a2 x 2 ... an x n Exponential function f ( z) 2 z Logarithm function f n ( x) log n ( x) Factorial function f (n) n! 24 School of Software 5.2 Functions for Computer Science 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 25 School of Software 5.2 Functions for Computer Science 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. 26 School of Software 5.2 Functions for Computer Science 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. Around 100 per list List #0 … 2011 List #1 … 193 1001 List #92 :2011193 1001… … List #100 customer account records 27 School of Software 5.2 Functions for Computer Science Fuzzy set and Fuzzy logic Refer to http://en.wikipedia.org/wiki/Fuzzy_mathematics 28 School of Software 5.2 Functions for Computer Science Homework Ex. 4, Ex. 7, Ex. 8, Ex.18, Ex. 22, Ex. 24 29 School of Software 5.3 Growth of Functions 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. | f ( n) | lim c, c R n | g ( n) | 30 School of Software 5.3 Growth of Functions Example 2 The function f(n) = ½ n3 + ½ n2 is O(g) for g(n)=n3. To see this, consider 1 3 1 2 1 3 1 3 n n n n n , if n 1 3 2 2 2 2 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). 31 School of Software 5.3 Growth of Functions 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 | f ( n) | c2 lim c1 c1 , c2 R n | g ( n) | 32 School of Software 5.3 Growth of Functions 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, 3n 4 5n 2 3n 4 5n 2 3n 4 5n 4 8n 4 , if n 1 Let c=8 and k=1, then |f(n)| <=c |g(n)| for all n>=k. Thus f is O(g). Conversely, n 4 3n 4 2n 4 3n 4 5n 2 , if n 2 Let c=1 and k=2, we have g is O(f). 33 School of Software 5.3 Growth of Functions 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) | f ( n) | lim 0 n | g ( n) | 34 School of Software 5.3 Growth of Functions 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. 35 School of Software 5.3 Growth of Functions 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) ) 36 School of Software 5.3 Growth of Functions 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 37 School of Software 5.3 Growth of Functions 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 38 School of Software 5.3 Growth of Functions 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 log a ( x) log b ( x) log a (b) in which loga(b) is a constant. Thus 1 | log b ( x) | | lg(n) |," " holds | lg(b) | and, conversely, | lg(n) | lg(b) | log b ( n) |," " holds Therefore g is O(f) and f is O(g). 39 School of Software 5.3 Growth of Functions 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) 40 School of Software 5.3 Growth of Functions 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) 41 School of Software 5.3 Growth of Functions 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) ? 42 School of Software 5.3 Growth of Functions Homework Ex. 6, Ex. 10, Ex. 12, Ex. 25, Ex. 29 43 School of Software 5.4 Permutation Functions 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. 44 School of Software 5.4 Permutation Functions 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: a1 , a2 , ..., an p p(a1 ), p(a2 ), ..., p(an ) 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. 45 School of Software 5.4 Permutation Functions Example 2 Let A={1,2,3}. Then all the permutations of A are 1 2 3 1 2 3 1 2 3 1A p1 p2 1 2 3 1 3 2 2 1 3 1 2 3 1 2 3 1 2 3 p3 p4 p5 2 3 1 3 1 2 3 2 1 46 School of Software 5.4 Permutation Functions Example 3 Using the permutations of Example 2, compute (a) p4-1 (b) p3o p2 (a) Viewing p4 as a function, we have 1 2 3 p4 P4={(1,3), (2,1), (3,2)} 3 1 2 then P4-1 ={(3,1), (1,2), (2,3)} 1 2 3 1 p 4 p3 2 3 1 47 School of Software 5.4 Permutation Functions (b) 1 2 3 1 2 3 p3 p2 2 3 1 2 1 3 1 2 3 3 2 1 48 School of Software 5.4 Permutation Functions 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. 49 School of Software 5.4 Permutation Functions 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 b1 b2 p(b2)= b3 br b3 … p(br-1)= br br-1 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) 50 School of Software 5.4 Permutation Functions Example 4 Let A={1,2,3,4,5}.The cycle (1,3,5) denotes the permutation 1 2 3 4 5 1 3 2 5 4 1 5 3 Note: (1, 3, 5) = (3, 5, 1) = ( 5,1, 3) 51 School of Software 5.4 Permutation Functions 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: 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 52 School of Software 5.4 Permutation Functions Example 5 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?) 53 School of Software 5.4 Permutation Functions 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. 54 School of Software 5.4 Permutation Functions 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: 12345678 p 34652187 p (7, 8) (2, 4, 5) (1, 3, 6) (2, 4, 5) (1, 3, 6) (7, 8) ... Note: the product is unique except for the order of the cycles 55 School of Software 5.4 Permutation Functions 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 56 School of Software 5.4 Permutation Functions 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) 57 School of Software 5.4 Permutation Functions Corollary 1 Every permutation of a finite set with at least two elements can be written as a product of transpositions 58 School of Software 5.4 Permutation Functions Example 9 Write the following permutation as a product of transposition 12345678 p 34652187 p (7, 8) (2, 4, 5) (1, 3, 6) (1, 3, 6) (1, 6) (1, 3) (2, 4, 5) (2, 5) (2, 4) p (7, 8) (2, 5) (2, 4) (1, 6) (1, 3) 59 School of Software 5.4 Permutation Functions Every permutation on a set of two or more elements can be written as a product of transpositions in many ways For instance (1, 2,3) (1,3) (1, 2) Even Transpositions (2,1) (2,3) Even Transpositions (1,3) (3,1) (1,3) (1, 2) (3, 2) (2,3) Even Transpositions 60 School of Software 5.4 Permutation Functions 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. 61 School of Software 5.4 Permutation Functions Example 10 Is the permutation 1234567 p 2457631 even or odd? p (3,5, 6) (1, 2, 4, 7) (1, 2, 4, 7) (1, 7) (1, 4) (1, 2) (3,5, 6) (3, 6) (3,5) p (3, 6) (3,5) (1, 7) (1, 4) (1, 2) Odd Transpositions 62 School of Software 5.4 Permutation Functions 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 63 School of Software 5.4 Permutation Functions 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. 64 School of Software 5.4 Permutation Functions 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 65 School of Software 5.4 Permutation Functions 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 66 School of Software 5.4 Permutation Functions Homework Ex. 2, Ex. 6, Ex.24, Ex. 26, Ex. 27, Ex. 37 67 School of Software