Discrete Structures Li Tak Sing (李德成) Lectures 9-10 1 Pigeonhole principle If m pigeons fly into n pigeonholes where m>n, then one pigeonhole will have two or more pigeons. 2 Examples in pigeonhole principle 1. How many people are needed in a group to be sure that three were born on the same day of the week? 2. How many people are needed in a group to be sured that four were born in the same month? 3. Why does any set of 10 nonempty strings over {a,b,c} contain two different strings whose starting agree and whose ending letters agree? 4. Find the size needed for a set of nonempty strings over {a,b,c,d} so that it must contain at least three strings whose starting letters agree and whose ending letters agree. 3 1. 27+1 2. 3 12+1 3. maximum number of strings with different starting and ending=3 3=9. Therefore if there are 10 strings, at least two of them must have the same starting and ending. 4. 4 4 2+1 4 The Mod function and inverses Let n>1 and let f:NnNn be defined as follows, where a and b are integers. f(x)=(ax+b)mod n. Then f is a bijection iff gcd(a,n)=1. When this is the case, the inverse function f-1 is defined by f-1(x)=(kx+c) mod n where c is an integer such that f(c)=0, and k is an integer such that 1=ak+nm for some integer m. 5 Proof 6 Example Fina a,bZ so that: 1. 2. 3. 4. 5. 10a+7b=1 5a-2b=1 -3a+4b=1 4a-5b=1 6a+7b=1 7 Solution 1. 2. 3. 4. 5. a=-2, b=3 a=1,b=2 a=1,b=1 a=-1,b=-1 a=-1,b=1 8 Simple ciphers N26={0,1,..25} represents letters {a,...,z} We can use a function to transform a letter to another. For example f: N26N26, f(x)=(x+5) mod 26 The inverse of f, denoted as f-1, is: f-1(x)=(x-5) mod 26 f is known as a cipher which would transform a letter into another letter to hide secret. 9 Multiplicative cipher The previous cipher is called an additive cipher because addition is use. A multiplicative cipher is one that multiplication is used. An example is: g(x)=3x mod 26 the inverse is g-1(x)=9x mod 26 10 Example Determine where the following functions define a cipher. If a function is a cipher, then find the inverse function 1. 2. 3. 4. 5. f(x)=2x mod 6 f(x)=2x mod 5 f(x)=5x mod 6 f(x)=(3x+2) mod 6 f(x)=(2x+3) mod 7 11 Solution 1. It is not a cipher because 2 and 6 are not relatively prime. 2. It is a cipher. f-1(x)=-2x mod 5 3. It is a cipher. f-1(x)=-x mod 6 4. No, it is not a cipher. 5. It is a cipher. f-1(x)=-3x +2mod 7 12 Hash function A hash function is a function that maps a set S of keys to a finite set of table indexes, which we'll assume are 0,1,...,n-1. A table whose information is found by a hash function is called a hash table. 13 Hash example For example, let S be the set of threeletter abbreviations for the months of the year. We might define a hash function f:S{0,1,..,11} in the following way. f(XYZ)=(ord(X)+ord(Y)+ord(Z))mod 12. where ord(X) represents the ASCII code for X. 14 One use of hash functions Assume that you have a table of m entry and you want to store some items in the table. Each item has a key which can be used to uniquely identify it. Now, we can use a hash function so that it will generate an index for the item to be stored in the corresponding entry. This method has an advantage that when we know of the key, we can find the position where the item is stored. 15 Collisions When two objects have the same key, a collision is said to occur. One technique to solve the problem of collision is called linear probing. With this technique the program searches the remaining locations in a linear manner. 16 Hash examples Let S={Monday, Tuesday, Wednesday, Thursday, Friday, Saturday, Sunday} and suppose that f:SN7 is the hash function defined by f(x)=lenght(x) mod 7 where length(x) is the number of letters in x. Use h to place each day of S into a hash table indexed by {0,1,..,6} starting with Monday, then Tuesday, and so on until Sunday. Resolve collisions by linear probing with a gap of 2. 17 Hash examples Let S={one,two,three, four, five, six, seven, eight, nine} and let f:SN9 be defined by f(x)=(3|x|) mod 9, where |x| means the number of letters in x. For each of the following gaps, construct a hash table that contains the strings of S by choosing a string for entry in the table by the order that it is listed in S. Resolve collisions by linear probing with the given gap and observe whether all strings can be placed in the table. Gap=1 Gap=2 Gap=3 18 Countability Let A and B be sets. If there is a bijection between A and B, we'll denote the fact by writing |A|=|B|. In this case, we'll say that A and B have the same size or have the same cardinality. 19 Finite sets examples Find the cardinality of the following function. 1. {2x+5|xZ and 12x+5100}. 2. {x2|x Z and 0x2 500} 3. {2,5,8,11,...,44,47} 20 Solution 1. 50 2. 23 3. 16 21 Cardinality of an infinite set Let Odd denote the set of odd natural numbers. Then the function f:NOdd defined by f(x)=2x+1 is a bijection. Therefore |N|=|Odd|. If there is an injection from A to B, we then |A||B|. If there is an surjection from B to A, we have |A||B|. If there is an injection from A to B, but no bijection between them, then |A|<|B|. 22 Sets that are countable A set is countable if it is finite or if there is a bijection between it and N. In the latter case, the set is said to be countably infinite. S is countable if |S|=n where n is a natural number or |S|=|N| 23 Countable Properties 1. Every subset of N is countable. 2. S is countable iff |S||N|. 3. Any subset of a countable set is countable. 4. Any image of a countable set is countable. 24 Proof of rule 1 If the subset is finite, then it is countable. If it is not finite, we can arrange the elements in ascending order and denote the elements as xo, x1, ...... So there is a bijection between {xo, x1,...} and N. 25 Proof of rule 2 If SN is countable, then either |S|=n or |S|=|N|. So |S||N|. If |S||N|, then, there is an injection f from S to N. Now, |S|=|f(S)|. Now, f(S) is a subset of N, by rule 1, f(S) is countable. Therefore f(S) is either finite or |f(S)|=|N|. This imply either S is finite or |S|=|N|. Both imply that S is countable. 26 Proof of rule 3 If A is a countable set, then there is an injection from A to N. Assume that f:AN be one of the injective functions. Now, for any subject B of A, we define the function g: B N such that g(x)=f(x). It is obvious that g is also an injection. Therefore B is also countable. 27 Proof of rule 4 If A is a countable set, then there is an injection from A to N. Let f:A N be an injection. Now, for a function g: A B for every yg(A), there is an x A such that g(x)=y. Now, we define a function h: g(A) A such that h(y)=x so that g(x)=y and f(x) is maximum. Now, h must be an injection because an element in A cannot map with two different elements in B under g. Therefore, fh is an injection as both f and h are injections. fh is a function from g(A) to N. So g(A) is countable. 28 NxN is countable (0,0) has 1 element (0,1), (1,0) have 2 elements (0,2),(1,1),(2,0) have 3 elements ..... (0,n), (1,n-1),.....(0,n) have n+1 elements 29 (0,0) maps to 0, (0,1), (1,0) map to 1,1+1 (0,2),(1,1),(2,0) map to (1+2),(1+2)+1,(1+2)+2 ..... (0,n), (1,n-1),.....(0,n) maps to (1+2+..n), (1+2+..n)+1,.....(1+2+...n)+n 30 (0,0) maps to 0, (0,1), (1,0) map to 1,2 (0,2),(1,1),(2,0) map to 3,4,5 ..... (0,n), (1,n-1),.....(0,n) maps to (n2+n)/2, (n2+n)/2,....., (n2+n)/2+n 31 Counting unions of countable sets If So, S1,....Sn,.... is a sequence of countable sets, then the union SoS1 .... Sn .... is a countable set. 32 Proof List the elements of Sn as xn0, xn1,..... If Sn is finite, then repeat one of the elements indefinitely so that we have an infinite sequence of elements. If So, S1,....Sn,.... is a finite sequence, then we repeat one of the set indefinitely so that we have an infinite sequence of sets. (m,n) of NN is associated with xmn The mapping is not bijective because some elements of NN is mapped to the same xmn. However, the mapping is surjective because every element of xmn is associated with (m,n). So we have |N|=|NN|| SoS1 .... Sn .... | and therefore SoS1 .... Sn .... is countable. 33 Countability of the rationals Q+ ={m/n | m,nN and m,n0 } We can associate each m/n with (m,n)NN. Therefore, |Q+||NN|=|N|. Therefore Q+ is countable. Similary Q- ={-m/n | m,nN and m,n0 } and is also countable. Therefore Q= Q+ {0} Q- should be countable. 34 Examples Show that each of the following sets is countable by establishing a bijection between the set and N. 1. 2. 3. 4. 5. 6. 7. The set of even natural numbers. The set of negative integers. The set of strings over {a} The set of strings over {a} that have even length. The set Z of integers. The set of odd integers. The set of even integers. 35 Solution 1. f(n)=2n 2. f(z)=-1-z if x 0 f (x ) 3. af(x - 1) otherwise 4. 5. if x 0 f (x) aaf(x - 1) otherwise x 2 when x is even f ( x) x 1 when x is odd 2 6. 7. x when x is odd f ( x) x 1 when x is even x when x is even f ( x) x 1 when x is odd 36