4 Functions Before studying functions we will first quickly define a more general idea, namely the notion of a relation. A function turns out to be a special type of relation. Definition: Let S and T be sets. A binary relation on SxT is any subset of SxT. A binary relation on S is any subset of SxS. Note: Often the word binary is omitted and we simply use the term relation. Definition: An n-ary relation is a subset of A 1 xa 2 x... A n. Note: These are used in relational databases. Representations of Relations Example: Let A = {a 1, a 2, a 3, a 4 } and B = {b 1, b 2 }. Define a relation on AxB by R = {(a 1, b 2 ), (a 2, b 1 ), (a 2, b 2 ), (a 4, b 2 )}. 42
We now define a special class of relations, namely functions. Definition: A function f : A B is a binary relation R f on AxB such that for each a A! b B such that (a, b) R f. The book s definition is equivalent: A function f from a set X to a set Y is a relation between the elements of X and Y with the property that each element of X is related to a unique element of Y. More definitions: X is called the domain of f and Y is called the co-domain of f. The set {y Y ( x X)(y = f(x))} is called the range of f or the image of X under f. Notation: f(x) = y f : x y. Examples of Functions First some special types of functions. successor function f : Z Z n n + 1 44
Theorem 4.1 Let f : R R be given by f(x) = 4x 1. Then f is 1-1. Proof: Suppose f(x 1 ) = f(x 2 ). We must show that x 1 = x 2. But clearly we have 4x 1 1 = 4x 2 1 4x 1 = 4x 2 x 1 = x 2. Question: Is the following function 1-1? f : Z Z n n 4 Answer: No, since f(1) = 1 and f( 1) = 1. Definition: A function f : A B is onto or a surjection if b B a A such that f(a) = b. Examples: (3), (4), (7) and (8) are onto. Note: If a function is onto, then its range equals its co-domain. Note: f : Z Z given by f(n) = 4n 1 is not onto. To be onto, there would have to be an integer n such that 4n 1 = 0. This is clearly not the case. 48
Theorem 4.2 Let f : R R be given by f(x) = 4x 1. Then f is onto. Proof: For any real number y, 4x 1 = y iff x = y + 1 4. Since y + 1 is a real number we are done. 4 Definition: A function f : A B is a bijection if it is both 1-1 and onto. It is also called a 1-1 correspondence. Definition: A bijection f : X X is called a permutation of X. Note: We will prove (at least part of) the following theorem later by induction. Theorem 4.3 Let X be a finite set and f : X X. Then f is 1-1 iff f is onto. Note: The theorem is false if X is infinite. 49
Definition: A function f : A B is invertible if f 1 : B A is a function. In this case, f 1 is called the inverse of f. Theorem 4.4 A function f : A B is invertible iff it is a bijection. Before studying the Pigeonhole Principle we consider a few special functions. 1. A partial function with domain A and co-domain B is any function from A to B, where A A. Note: For x A A, f(x) is undefined. Example: f : R R x 1 x is a partial function (not defined for x = 0). Example: f : R R x x is a partial function (not defined for x < 0). 51
Pigeonhole Principle Pigeonhole Principle: Let A and B be finite sets. If f : A B and A > B 1, then there exists a 1 a 2 A such that f(a 1 ) = f(a 2 ), i.e., there does not exist an injection from A to B. Note: Actually, A can be infinite. Simple examples: 1. If there are 5 pigeons in 4 pigeonholes then at least one pigeonhole contains 2 or more pigeons. 2. In any group of thirteen people at least 2 were born in the same month. 3. A drawer contains 10 black socks and 10 white socks. What is the least number of socks you must pull out to be sure to get a matched pair? Answer: 3 4. If five points are chosen from a unit square, then some pair 1 are distance at most apart. 2 5. At any party there are always at least two people with the same number of acquaintances. 53
Reason: Let A be the set of n people at the party. Let B = {0, 1,..., n 1} be the set representing the possible number of acquaintances of any person at the party. Let f be the function that assigns, for each person at the party, the number of acquaintances of that person. If we let I f denote the image of f, then it is not possible to have both 0 and n 1 in I f, since if a person knows everyone it is not possible for some other person to know no one. Hence I f n 1. Since A = n, by the pigeonhole principle, f can not be a 1 1 function. Hence at least 2 people at the party must have the same number of acquaintances. 54
6. Let A = {1, 2, 3, 4, 5, 6, 7, 8}. If 5 integers are selected from A, then at least one pair of the integers must sum to nine. Reason: This follows since A can be partitioned into the 4 sets {1, 8}, {2, 7}, {3, 6}, {4, 5}. For 1 i 4, let B i = {i, 9 i}. Let S be any set of 5 integers from A and B be the 4 element set consisting of the sets B i, 1 i 4. Finally, consider the function f : S B which takes an element of S to the set containing that element. Since S = 5 and there are only 4 sets, by the pigeonhole principle, f is not a 1 1 function. Hence 2 of the elements of S are sent to the same set B i and these elements sum to 9. Note: If only 4 integers are chosen, then it is possible that no 2 of them will add up to 9. 55
We now look at a much more difficult application. Theorem 4.5 In any sequence of n 2 + 1 distinct integers there exists either 1. an increasing subsequence of length at least n + 1 or 2. a decreasing subsequence of length at least n + 1. The proof uses the pigeonhole principle. Before giving the proof we illustrate what the theorem is saying. Example: Let n = 3 and consider the sequence 3 1 2 10 4 14 9 7 8 6. 1 2 4 7 8 is an increasing subsequence of length 5. 14 9 8 6 is a decreasing subsequence of length 4. Example: Let n = 2 and consider the sequence 2 1 5 4 3. 5 4 3 is a decreasing subsequence of length 3. There is no increasing subsequence of length 3. 56
Proof: By contradiction (using the pigeonhole principle). Let a 1, a 2,..., a n 2 +1 be a sequence of distinct integers and assume the theorem is false for this sequence. Assign to each a k an ordered pair (x k, y k ), where 1. x k is the length of a longest increasing subsequence starting at a k. 2. y k is the length of a longest decreasing subsequence starting at a k. e.g. 2 1 5 4 3. a 1 (2, 2), a 2 (2, 1), a 3 (1, 3), a 4 (1, 2), a 5 (1, 1). Note: Since we are assuming the theorem is false, there are at most n 2 distinct (x k, y k ) pairs that are possible. Let A = {a k } A = n 2 + 1. B = {(x k, y k )} B = n 2. Let f : A B be the function that represents the above assignment. 57
Since A > B, by the pigeonhole principle we must have f(a i ) = f(a j ) for some i j, i.e., (x i, y i ) = (x j, y j ). Assume, without loss of generality, that i < j. Either a i < a j or a j < a i. Case 1: a i < a j. Then x i > x j, a contradiction. Case 2: a i > a j. Then y i > y j, a contradiction. Generalized Pigeonhole Principle: Suppose n objects are placed in k boxes. Then at least one box contains at least n/k objects. Example: Among 100 people, at least 9 were born in the same month. Note: 100/12 = 9. Note: The Pigeonhole Principle has k = n 1 and n/n 1 = 2. 58
Example: f : N N n n g : {0, 1, 2} N n n Here gof is not defined, e.g., what is gof(10)? However fog is defined, and in fact fog = g. 60
Example: f : N N n n/2 if n is even n 0 if n is odd. g : N N n 2n Now gof : N N n n if n is even n 0 if n is odd. fog : N N n n Note: In general, fog gof. 61
Theorem 4.6 Suppose f : A B and g : B C. If 1. f and g are surjective, then gof is surjective. 2. f and g are injective, then gof is injective. 3. f and g are bijective, then gof is bijective. Proof: First note that (3) easily follow from (1) and (2). 1. Let c C. Since g is onto b B such that g(b) = c. But since f is onto a A such that f(a) = b. Hence Thus gof is surjective. gof(a) = g(b) = c. 2. Suppose a 1 a 2 A. Since f is 1-1, f(a 1 ) f(a 2 ). Now since g is 1-1, g(f(a 1 )) g(f(a 2 )). Hence gof is 1-1. 62
Example: 1. f({1, 7, 13}) = {1} 2. f({1, 4, 5}) = {1, 2} f : N N n n mod 3 3. f 1 ({0}) = {m m = 3k for some k N} Theorem 4.9 Let f : A B with A 1, A 2 A. Then 1. f(a 1 A 2 ) = f(a 1 ) f(a 2 ) 2. f(a 1 A 2 ) f(a 1 ) f(a 2 ). Note: The above theorem holds for arbitrary (more than 2) unions. Example: f : R R x x 2 Let A 1 = R +. Then f(a 1 ) = R +. Let A 2 = R. Then f(a 2 ) = R +. Now A 1 A 2 = R {0} and A 1 A 2 =. f(a 1 A 2 ) = f(a 1 ) f(a 2 ) = R + = f(a 1 A 2 ) f(a 1 ) f(a 2 ) = R + 66
5 Cardinality Suppose A and B are finite sets and we wish to determine if the sets have the same size. Here are two ways this can be done. 1. Count the elements in A and then count the elements in B. 2. Create a correspondence between the elements of A and B, i.e., let the first element of A correspond to the first element of B, then the second, etc. Note: If A and B are infinite sets, then (1) can not be done. However (2) can still be done, and that is in fact how we compare the size (or cardinality) of the sets. Definition: We say the elements of set A are in a 1-1 correspondence with the elements of set B if there exists a 1-1, onto function between A and B. If this is the case we say A and B have the same cardinality or have the same size. Definition: A set is finite if it is either empty or it can be put into a 1-1 correspondence with a set of the form [n] = {1, 2,..., n}. Otherwise the set is infinite. 67
Example: A = {a, b, c} and [3] = {1, 2, 3} have the same cardinality. 1 a 2 b 3 c Definition: A set A is called countably infinite if it has the same cardinality as the set of positive integers Z +. A set is called countable if it is either finite or countably infinite. Note: An equivalent definition, and one that appears in many other books, is that a set is countably infinite if there is a 1-1 correspondence between it and the natural numbers N = {0, 1, 2,...}. We will use Z +, but either can be used to show a set is countably infinite. Example: Z + and N have the same cardinality. 1 0 2 1 3 2.. 68
Example: The set of nonnegative even integers forms a countably infinite set. 0 0 1 2 2 4.. Example: The set of all integers (Z) is a countably infinite set. The function f : Z + Z gives the 1-1 correspondence. We get f(n) := n 2 1 n 2 if n is an even positive integer if n is an odd positive integer f(1) = 0 f(2) = 1 f(3) = 1 f(4) = 2 f(5) = 2 f(6) = 3. =. Example: The set of all positive rationals, denoted Q +, is a countably infinite set. 69
Notation: The cardinality of a set A is given by A. We use χ 0 to denote the cardinality of Z +. Note: Intuitively, any set whose elements can be listed in some way is a countably infinite set. Question: Are there infinite sets that are not countably infinite? Answer: Yes. The set of all real numbers between 0 and 1 (J = [0, 1]) is not a countably infinite set. We call such sets uncountably infinite. Theorem 5.1 J = [0, 1] is uncountably infinite. Proof: We will assume J is countably infinite and reach a contradiction. We first must decide how a number x J will be represented. We will represent such a number as a decimal, i.e., x =.a 1 a 2 a 3.... We will not allow our decimals to end with an infinite sequence of 9 s, i.e., we use.25000... instead of.24999.... Now every number in J has a unique decimal expansion. Now suppose it is possible to list the numbers in J, i.e., that J is countably infinite and such a list can be described. We will produce a number x J that is not on the list. 71
Let a i,j represent the j th digit of the i th number on the list e. g. Thus a 2,3 = 4 and a 5,6 = 3. 1..2 3 1 2 4 7... 2..3 1 4 6 7 7... 3..2 9 8 6 4 1... 4..3 7 1 1 4 2... 5..6 6 1 2 6 3... 6..7 1 2 3 4 5.... We now construct a number x that is not on the list. If the i th digit of number i is not 1, then the i th digit of x is 1. If the i th digit of number i is 1, then the i th digit of x is 2. Using the example above, x =.121211.... Thus x always differs from number i in the i th digit. Hence x J is not on the list. This is a contradiction. 72
Application to Computer Science Let L be a fixed computer language. Question: How many different computer programs can be written in L? Theorem 5.3 The set of all programs that can be written in any language L is a countably infinite set. Proof: Each symbol in a computer language is translated into a string of 0 s and 1 s. Hence any program can be thought of as a long string of 0 s and 1 s. To show that this is a countable set we must find a way to list these sequences of 0 and 1 s. The first thing to do is to list them by length. If two strings have the same length, they should be ordered in ascending order according to the binary number they represent. Thus 0010 precedes 1010 in the ordering. Since all the programs in L are on the list, there are countably many such programs. Note: We have just shown that for any alphabet Σ, the set Σ is countably infinite. In particular, if Σ = {0, 1}, we see that the set of finite binary strings is countably infinite. Note: The infinite binary strings over Σ = {0, 1} is an uncountably infinite set. This can be shown with a diagonalization argument. 74
We now use this result to show that there are noncomputable functions. Let T be the set of all functions f : Z + {0, 1, 2, 3, 4, 5, 6, 7, 8, 9} We will show that T is uncountably infinite. Thus, since the set of all programs in any computer language L is countably infinite, there are not enough programs to compute every function in T. Theorem 5.4 Let T be the set of all functions f : Z + {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}. Then T is uncountably infinite. The proof of the theorem assumes the following intuitive result. Theorem 5.5 Any subset of a countable set is countable. Proof of Theorem 5.4: Again let S = {x R 0 < x < 1}. As before we can represent any number in S uniquely as a decimal.a 1 a 2 a 3..., provided we do not use decimals that end in an infinite sequence of 9 s. Now define a function F from S to a subset of T by letting F (.a 1 a 2 a 3...) = the function that sends each positive integer n to a n. Define the co-domain of F to be the subset of T that makes F onto. Now F is 1-1 and onto. Thus, since S is uncountable, so is the co-domain of F. But then T has an uncountable subset and so must also be uncountable. 75