Presentation is loading. Please wait.

Presentation is loading. Please wait.

Discrete Math for CS Chapter 5: Functions. Discrete Math for CS New Relation Operations: Given R, a relation on A x B, we define the inverse relation,

Similar presentations


Presentation on theme: "Discrete Math for CS Chapter 5: Functions. Discrete Math for CS New Relation Operations: Given R, a relation on A x B, we define the inverse relation,"— Presentation transcript:

1 Discrete Math for CS Chapter 5: Functions

2 Discrete Math for CS New Relation Operations: Given R, a relation on A x B, we define the inverse relation, R -1, as follows: Examples: R -1 = {(b,a): (a,b) ∈ R} If R is “is_parent_of” then R -1 is “is_child_of” In graphical terms, the inverse of a relation expressed as a digraph is the same graph with all the arrows reversed.

3 Discrete Math for CS Another Relation Operation: Let R be relation on A x B and S be a relation on B x C. The composition of R and S, written S ◦ R, is a binary relation on A x C defined as follows: Examples: S ◦ R = { (a,c): a ∈ A, c ∈ C while aRb and bRc for some b ∈ B} Suppose R is the relation “is_sister_of” and S is the relation “is_mother_of” then determine S ◦ R, S ◦ S and R ◦ R. S ◦ R is the relation “is_aunt_of” S ◦ S is the relation “is_grandmother_of” R ◦ R is the relation “is_sister_of”

4 Discrete Math for CS A Graph Example: R = {a,b}, S = {1,2,3} and C = {x,y}. a b x y 1 2 3 a b x y RS S ◦ R

5 Discrete Math for CS Composition as a Matrix Multiplication: Let A = {a 1, a 2,..., a n }, B = {b 1, b 2,..., b m } and C = {c 1, c 2,..., c k }. Let R be a relation on A x B and S be a relation on B x C. The matrices representing R and S are: The matrix representing S ◦ R, called P is: M = [m ij ] where m ij = 1 if (a i,b j ) ∈ R; 0 otherwise N = [n ij ] where n ij = 1 if (b i,c j ) ∈ S; 0 otherwise P = [p ij ] where p ij = [(m i1 and n 1j ) or (m i2 and n 2j ) or... (m in and n nj )] = 1 if a i R b k and b k S c j for at least one k = 0 otherwise in which case we say a i (S ◦ R) c j.

6 Discrete Math for CS A Matrix Example; R = {a,b}, S = {1,2,3} and C = {x,y}. M R = 1 1 1 0 1 0 〚〛 M S = 0 1 1 0 〚〛 M S◦R = 1 1 0 〚〛

7 Discrete Math for CS Another Example: Let A = {1, 2, 3, 4, 5} and R be a relation of A x A defined by the following matrix. If R were transitive, R ◦ R would be a subset of R and we see that it isn't. So R is not transitive. M R = 0 0 1 1 0 0 1 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0   M R◦ R = 0 0 1 1 0 0 1 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0   0 0 1 1 0 0 1 0 0 1 0 0 0 1 0 0 0 1 0 0 0 1 0 0 0   0 0 1 1 0 0 1 0 0 1 0 0 1 0 0 0 0 0 1 0 0 1 0 0 1   = If R is transitive then whenever xRy and yRz we know xRz. In other words, if m xy = 1 and m yz = 1 then m xz = 1. m 43 = 1m 34 = 1m 44 = 1

8 Discrete Math for CS Functions: A function, f, from a set A to a set B is a binary relation R f on A x B such that for every a ∈ A, there is a unique b ∈ B such that b = f(a) (ie, aR f b). From the point of view of digraphs; only one arrow leaves each node labeled by an element of A. 1 2 3 x y RfRf A B

9 Discrete Math for CS Examples: Which if the following relations are functions: The relation x is a brother or sister of y on the set P of people. The relation on ℤ given as {(x,x 2 ): x ∈ ℤ } The relation on ℝ given as {(x,y): x = y 2 }

10 Discrete Math for CS Domain and Range If f is a function from A to B then for every a in A, there is a b in B such that b = f(a) (ie (a,b) ∈ f). b is called the image of a under f. We call A the domain of f, B the codomain of f and f(A) the range of f. A B f(A)‏ f

11 Discrete Math for CS Graphs of functions If A and B are infinite sets we can't draw digraphs so we plot the graph on any function on A and B Example: f: ℝ ℝ, given by f(x) = x 2. (2,4)‏ The graph of y = f(x) is a subset of the real plane, ℝ 2.

12 Discrete Math for CS Exercise: Plot the graph of g(x) = 2 – x and identify the point (2, g(2)). (2,0)‏ (0,2)‏

13 Discrete Math for CS Injective Functions: Let f: A → B be a function. We say that f is injective (or 1-1) if whenever f(a 1 ) = f(a 2 ) ⇒ a 1 = a 2 for all a 1, a 2 ∈ A. Contrapositively this is the same as saying a 1 ≠ a 2 ⇒ f(a 1 ) ≠ f(a 2 ), for all a 1, a 2 ∈ A. abcabc 123123 abcabc 123123 abcabc 123123 abab 123123 Which functions are injective?

14 Discrete Math for CS Surjective Functions: Let f: A → B be a function. We say that f is surjective (or onto) if the range and codomain of f coincide. abcabc 123123 abcabc 123123 abcabc 123123 abab 123123 Which functions are surjective?

15 Discrete Math for CS Bijective Functions: Let f: A → B be a function. We say that f is bijective (or 1-1 and onto) if f is both injective and surjective. abcabc 123123 abcabc 123123 abcabc 123123 abab 123123 Which functions are bijective?

16 Discrete Math for CS Exercise: Show h: ℤ → ℤ defined by h(x) = x 2 is neither injective nor surjective. not injective: sufficient to find two values, a 1, a 2 ∈ ℤ where a 1 ≠ a 2 but h(a 1 ) = h(a 2 )‏ not surjective: sufficient to find a value, a 1 ∈ ℤ that is not in the range of h.

17 Discrete Math for CS Exercise: Show f: ℝ ℝ, given by f(x) = 4x + 3 is bijective. (0,3)‏ (-3/4,0)‏ horizontal line test

18 Discrete Math for CS Inverse Functions: Let f: A → B be a function. Define the relation f -1 : B → A as f -1 (b) = a if f(a) = b. If f -1 is a function we call it the inverse of f. Which functions are invertible? Reverse the arrows and ask if the new relations are functions? abcabc 123123 abcabc 123123 abcabc 123123 abab 123123

19 Discrete Math for CS Inverse Functions: Let f: A → B be a function. Define the relation f -1 : B → A as f -1 (b) = a if f(a) = b. If f -1 is a function we call it the inverse of f. Which functions are invertible? Reverse the arrows and ask if the new relations are functions? abcabc 123123 abcabc 123123 abcabc 123123 abab 123123

20 Discrete Math for CS Inverse Function Formulae: y = 3x – 2 is a function, what is its inverse? The answer is a bit like the “reversing arrows” answer of the previous slide. Reverse the arrows and operations: Exchange x and y in the original equation and solve for y. x = 3y – 2 x + 2 = 3y (x + 2)/3 = y y = (x+2)/3 is the inverse of the original function. times 3minus 2 x3x 3x - 2 divide by 3 add 2 (x+2)/3 x + 2x

21 Discrete Math for CS Inverse Function Theorem: Let f: A → B be a function. f -1 is a function if and only f is both injective and surjective. A bijective function is invertible: f = {(a,b): a ∈ A, b ∈ B} while f -1 = {(b,a): b ∈ B, a ∈ A} Since f is surjective, every b ∈ B appears as at least one f(a). Since f is injective, there is only one such a. Hence the pairs (b,a) ∈ f -1 have the property that for every b ∈ B, there is only one a ∈ A such that a = f -1 (b). Thus f -1 is a function. An invertible function is bijective: Since f is invertible, for every b ∈ B there is a unique a A such that (b,a) ∈ f -1. This (a,b) ∈ f and this shows f is surjective. Suppose f(a 1 ) = f(a 2 ) = b. Then (b,a 1 ) and (b,a 2 ) belong to f -1. However, f -1 is a function and so a 1 = a 2. Thus f is injective.

22 Discrete Math for CS Example: Let A = { x: x ∈ ℝ, x = 1} and f: A A given by Show f is bijective and calculate its inverse. To calculate the inverse, switch x and y and solve for y. y = f(x) = x / (x-1) If a 1 /(a 1 – 1) = a 2 /(a 2 – 1) we know a 1 a 2 – a 1 = a 2 a 1 – a 2 a 1 = a 2. Hence f is injective. For any a ∈ A we must now solve a = b/(b-1). It is easy to see that b = a/(a-1) and since a 1, this is a real number. Hence f is surjective. x = y / (y-1)‏ x(y-1) = y xy -x = y xy – y = x y(x-1) = x y= x/(x-1) so f -1 (x) = x/(x=1)‏

23 Discrete Math for CS Composition: Let f: A → B and g: B → C be two functions. The composite function g ∘ f: A → C is defined as {(a,c): where there is a b ∈ B such that (a,b) ∈ f and (b,c) ∈ g}. Since b, above, exists and is unique given a, g ∘ f is a function.

24 Discrete Math for CS Examples: Consider f: ℝ ℝ, given by f(x) = x 2 and g: ℝ ℝ  4x + 3. Calculate g ∘ f, f ∘ g, g ∘ g and f ∘ f. g ∘ f(x) = g(f(x)) = 4x 2 + 3 f ∘ g(x) = f(g(x)) = (4x+3) 2 = 16x 2 + 24x + 9 g ∘ g(x) = f(g(x)) = 4(4x+3) + 3 = 16x + 15 f ∘ f(x) = f(f(x)) = (x 2 ) 2 = x 4

25 Discrete Math for CS Functions and Programming: Functions are the basis, in programming, of the common divide- and-conquer strategy of dealing with program complexity. Functions also are the basis of member methods in OOP. Program functions often have “side effects”. x = 3; is a function that returns the value of the right-hand-side as its value. As a side effect it assigns a new value to x. What does the function x++; return? What is its side effect?

26 Discrete Math for CS Functional Programming Languages: Programming languages exist that depend heavily on the concept of composition of functions. We'll look at one at the end of the chapter.

27 Discrete Math for CS Pigeon Hole Principle: Let A and B be two finite sets, |A| > |B| and f: A → B a function from A to B. The Pigeon Hole Principle says that there exist at least two distinct elements of A, a 1, a 2 ∈ A, a 1 != a 2 but f(a 1 ) = f(a 2 ). a1a1 a2a2 A B b If A = {a 1,..., a n } then { f(a 1 ),..., f(a n ) } B. So | { f(a 1 ),..., f(a n ) } | <= |B| <|A|. So there are fewer elements in { f(a 1 ),..., f(a n ) } than in A. This can only happen if at least two of the images under f are the same.

28 Discrete Math for CS 10 pigeons, 9 holes means 2 pigeons in 1 hole

29 Discrete Math for CS Example 1: There are 15 people on a bus. Show that at least two of them are born in the same month. Answer: PHP People Months birth month

30 Discrete Math for CS Observation: That wasn't so hard. We could have done that without a lot of mathematics. It turns out that not all problems that can be solved using PHP are as easy to recognize.

31 Discrete Math for CS Example 2: How many last names must appear in a phonebook in order to guarantee that at least two of the last names must have the same first letter and the same last letter? Let A be the set of people's last names. Let B be the set of pairs of letters from the alphabet. |B| = 26*26 = 676 In order to use the PHP we must have |A| > 676. AB f(last name) =

32 Discrete Math for CS Example 3: Five numbers are selected from the set of numbers 1, 2,..., 8. Show that there will always be at least two of the numbers that sum to 9. The pairs that sum to 9 are {1,8}, {2,7}, {3,6}, {4,5}. {1,8} {2,7} {3,6} {4,5} B A = any 5 numbers from 1 to 9 a1a2a3a4a5a1a2a3a4a5 f f: map the number to the set it belongs to PHP: At least two numbers must map to the same set.

33 Discrete Math for CS Generalization of PHP: Suppose A and B are two finite sets such that |A| > k|B| for some positive integer k. Suppose f: A → B is a function from A to B. Some element of B appears as the image of at least k+1 elements of A. Reasoning: If this were not true; each image under f would appear at most k times and so |A|/k < |B|. Hence |A| < k|B|, contradiction.

34 Discrete Math for CS Example 4: How many names must appear in a phone book in order to guarantee that at least 5 of the names begin with the same letter and end with the same letter. Go back to f: A → B of Example 2. B contains 676 elements. In order to 5 names to begin and end with the same letters we need to have |A| > 4|B|. In other words, at least 2705 names in the phone book.

35 Discrete Math for CS Example 5: In any group of 6 people, show there are either 3 people who all know each other or 3 complete strangers. Let x be one of the six people. Let A be the other 5. Let B = {0,1}. Consider the function f: A → B given by f(a) = 1 if a knows x f(a) = 0 if a does not know x. Since |A| > 2|B|, there must be - either three people who know x (f(a) = 1) or - three people who do not know x (f(a) = 0). Case 1 (three people, say a, b and c know x): Either two of a, b and c know each other. In which case, these two together with x, make three people who know each other. Or none of the three who know x, know each other so there is a set of 3 people who are mutual strangers. Case 2: (three people, a, b and c do not know x): Either two of a, b and c do not know each other. In which case, these two together with x, make three people who do not know each other.Or all three know each other so make a set of three people who know each other.

36 Discrete Math for CS Unexpected Implications: There are at least two people in NYC with the same number of hairs in their heads.

37 Discrete Math for CS Unexpected Implications: If 187 people meet at a party and there is a general shaking of hands all round, everyone greeting lots of people. Show there must be at least two people at the party who shake hands with the same number of people.

38 Discrete Math for CS Computer Science Uses of PHP In a hash table collisions occur when two different values hash to the same location in the table. Suppose we have a lossless compression algorithm that is used to reduce the size of input files. There must be at least one file where the algorithm actually produces a bigger file and not a smaller file


Download ppt "Discrete Math for CS Chapter 5: Functions. Discrete Math for CS New Relation Operations: Given R, a relation on A x B, we define the inverse relation,"

Similar presentations


Ads by Google