1 / 67

Chapter 5. Functions

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. 5.1. Functions.

jeslyn
Download Presentation

Chapter 5. Functions

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 5. Functions Weiqi Luo (骆伟祺) School of Software Sun Yat-Sen University Email:weiqi.luo@yahoo.com Office:A309

  2. Chapter five: Functions • 5.1. Functions • 5.2. Functions for Computer Science • 5.3. Growth of Functions • 5.4. Permutation Functions

  3. 5.1. Functions • Function (mapping, transformation) Let A and B be nonempty sets. A function f from A to B, which is denoted f: AB, 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 argument a or the image of a under f b=f(a) a A B

  4. 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.

  5. 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}

  6. 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 fpdefined 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

  7. 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: VL g: EL e.g. Boston 44 49 Worcester 51 64 Providence Hartford 106 39 New Haven

  8. 5.1 Functions • Example 7 Let A=Z and B={0,1}. Let f: AB 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

  9. 5.1 Functions • Composition Let f: AB and g: BC are functions. Then the composition of f and g, gof, is a relation from A to C. gof f g a c=f(b) = gof (a) b=f(a) B A C

  10. 5.1 Functions • Example 9 Let A=B=Z, and C be the set of even integers. Let f: AB, and g: BC 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)

  11. 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

  12. 5.1 Functions • Example 11 Let A=B=Z and let f: AB 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’

  13. 5.1 Functions • Invertible A function f: AB 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}

  14. 5.1 Functions • Theorem 1 Let f: AB 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

  15. 5.1 Functions • Example 15 Let A=B=Z and let f: AB 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.

  16. 5.1 Functions • Theorem 2 Let f: AB be any function. Then (a) 1Bo 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

  17. 5.1 Functions • Theorem 3 (a) Let f :AB and g: BA 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 : AB and g: BC be invertible. The g o f is invertible, and (g o f) -1 =f-1 o g-1

  18. 5.1 Functions • Theorem 4 Let A and B be two finite sets with the same number of elements, and let f: AB 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)

  19. 5.1 Functions • Example 18 CKAJADPDGKJYEIDOT JOHNHASALONGBEARD

  20. 5.1 Functions • Homework Ex. 2, Ex. 10, Ex. 20, Ex. 32, Ex. 36, Ex.41

  21. 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: 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

  22. 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

  23. 5.2 Functions for Computer Science • 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

  24. 5.2 Functions for Computer Science • Polynomial function • Exponential function • Logarithm function • Factorial function

  25. 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

  26. 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.

  27. 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 193 1001 List #1 … List #92 :2011193 1001… … List #100 customer account records

  28. 5.2 Functions for Computer Science • Fuzzy set and Fuzzy logic Refer to http://en.wikipedia.org/wiki/Fuzzy_mathematics

  29. 5.2 Functions for Computer Science • Homework Ex. 4, Ex. 7, Ex. 8, Ex.18, Ex. 22, Ex. 24

  30. 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.

  31. 5.3 Growth of Functions • 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).

  32. 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

  33. 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, 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).

  34. 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)

  35. 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.

  36. 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) )

  37. 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

  38. 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

  39. 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 in which loga(b) is a constant. Thus and, conversely, Therefore g is O(f) and f is O(g).

  40. 5.3 Growth of Functions Rules for determining the Θ-Class of a Function • Θ(1) functions are constant and have zero growth, the slowest growth possible. • Θ(lg(n)) is lower than Θ(nk) if k>0 • Θ(na) is lower than Θ(nb) iff b>a>0 • Θ(an) is lower than Θ(bn) iff b>a>0 • Θ(nk) is lower than Θ(an) for any power nk and any a>1 • If r is a not zero, and Θ(rf)= Θ(f) for any function f • 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) ) • If Θ(f) is lower than Θ(g), then Θ(f+g) = Θ(g)

  41. 5.3 Growth of Functions • Example 7 Determine the Θ-class of each of the following • f(n) = 4n4-6n7+24n3 • g(n) = lg(n) -3n • 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)

  42. 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) ?

  43. 5.3 Growth of Functions • Homework Ex. 6, Ex. 10, Ex. 12, Ex. 25, Ex. 29

  44. 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: AA be defined by f(a)=2a+1. Since f is one to one and onto, it follows that f is a permutation of A.

  45. 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: 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.

  46. 5.4 Permutation Functions • Example 2 Let A={1,2,3}. Then all the permutations of A are

  47. 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  P4={(1,3), (2,1), (3,2)} then P4-1 ={(3,1), (1,2), (2,3)}

  48. 5.4 Permutation Functions (b)

  49. 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.

  50. 5.4 Permutation Functions • Cyclic Permutation Let b1, b2, …, br be distinct elements of the set A={a1,a2,…,an}. The permutation p: AA 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) b1 b2 br b3 br-1

More Related