36
Discrete Structures Li Tak Sing ( 李李李 ) Lectures 9-10 1

Discrete Structures Li Tak Sing ( 李德成 ) Lectures 9-10 1

Embed Size (px)

Citation preview

Page 1: Discrete Structures Li Tak Sing ( 李德成 ) Lectures 9-10 1

Discrete Structures

Li Tak Sing (李德成 )

Lectures 9-10

1

Page 2: 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

Page 3: Discrete Structures Li Tak Sing ( 李德成 ) Lectures 9-10 1

Examples in pigeonhole principle

1. How many people are needed in a group to be sure that three people 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

Page 4: Discrete Structures Li Tak Sing ( 李德成 ) Lectures 9-10 1

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

Page 5: Discrete Structures Li Tak Sing ( 李德成 ) Lectures 9-10 1

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 nwhere c is an integer such that f(c)=0, and k is an integer such that 1=ak+nm for some integer m.

5

Page 6: Discrete Structures Li Tak Sing ( 李德成 ) Lectures 9-10 1

Proof

6

Page 7: Discrete Structures Li Tak Sing ( 李德成 ) Lectures 9-10 1

Example

Fina a,bZ so that:1. 10a+7b=1

2. 5a-2b=1

3. -3a+4b=1

4. 4a-5b=1

5. 6a+7b=1

7

Page 8: Discrete Structures Li Tak Sing ( 李德成 ) Lectures 9-10 1

Solution

1. a=-2, b=3

2. a=1,b=2

3. a=1,b=1

4. a=-1,b=-1

5. a=-1,b=1

8

Page 9: Discrete Structures Li Tak Sing ( 李德成 ) Lectures 9-10 1

Simple ciphers

N26={0,1,..25} represents letters {a,...,z}We can use a function to transform a

letter to another. For examplef: N26N26, f(x)=(x+5) mod 26The 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

Page 10: Discrete Structures Li Tak Sing ( 李德成 ) Lectures 9-10 1

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 26the inverse is g-1(x)=9x mod 26

10

Page 11: Discrete Structures Li Tak Sing ( 李德成 ) Lectures 9-10 1

Example

Determine where the following functions define a cipher. If a function is a cipher, then find the inverse function1. f(x)=2x mod 6

2. f(x)=2x mod 5

3. f(x)=5x mod 6

4. f(x)=(3x+2) mod 6

5. f(x)=(2x+3) mod 7

11

Page 12: Discrete Structures Li Tak Sing ( 李德成 ) Lectures 9-10 1

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

Page 13: Discrete Structures Li Tak Sing ( 李德成 ) Lectures 9-10 1

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

Page 14: Discrete Structures Li Tak Sing ( 李德成 ) Lectures 9-10 1

Hash example

For example, let S be the set of three-letter 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

Page 15: Discrete Structures Li Tak Sing ( 李德成 ) Lectures 9-10 1

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

Page 16: Discrete Structures Li Tak Sing ( 李德成 ) Lectures 9-10 1

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

Page 17: Discrete Structures Li Tak Sing ( 李德成 ) Lectures 9-10 1

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

Page 18: Discrete Structures Li Tak Sing ( 李德成 ) Lectures 9-10 1

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=1Gap=2Gap=3

18

Page 19: Discrete Structures Li Tak Sing ( 李德成 ) Lectures 9-10 1

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

Page 20: Discrete Structures Li Tak Sing ( 李德成 ) Lectures 9-10 1

Finite sets examples

Find the cardinality of the following function.1. {2x+5|xZ and 12x+5100}.

2. {2,5,8,11,...,44,47}

20

Page 21: Discrete Structures Li Tak Sing ( 李德成 ) Lectures 9-10 1

Solution

1. 50

2. 16

21

Page 22: Discrete Structures Li Tak Sing ( 李德成 ) Lectures 9-10 1

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

Page 23: Discrete Structures Li Tak Sing ( 李德成 ) Lectures 9-10 1

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

Page 24: Discrete Structures Li Tak Sing ( 李德成 ) Lectures 9-10 1

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

Page 25: Discrete Structures Li Tak Sing ( 李德成 ) Lectures 9-10 1

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

Page 26: Discrete Structures Li Tak Sing ( 李德成 ) Lectures 9-10 1

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 implies either S is finite or |S|=|N|. Both imply that S is countable.

26

Page 27: Discrete Structures Li Tak Sing ( 李德成 ) Lectures 9-10 1

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

Page 28: Discrete Structures Li Tak Sing ( 李德成 ) Lectures 9-10 1

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

Page 29: Discrete Structures Li Tak Sing ( 李德成 ) Lectures 9-10 1

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

Page 30: Discrete Structures Li Tak Sing ( 李德成 ) Lectures 9-10 1

(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

Page 31: Discrete Structures Li Tak Sing ( 李德成 ) Lectures 9-10 1

(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

Page 32: Discrete Structures Li Tak Sing ( 李德成 ) Lectures 9-10 1

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

Page 33: Discrete Structures Li Tak Sing ( 李德成 ) Lectures 9-10 1

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

Page 34: Discrete Structures Li Tak Sing ( 李德成 ) Lectures 9-10 1

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

Page 35: Discrete Structures Li Tak Sing ( 李德成 ) Lectures 9-10 1

Examples

Show that each of the following sets is countable by establishing a bijection between the set and N.1. The set of even natural numbers.

2. The set of negative integers.

3. The set of strings over {a}

4. The set of strings over {a} that have even length.

5. The set Z of integers.

6. The set of odd integers.

7. The set of even integers.

35

Page 36: Discrete Structures Li Tak Sing ( 李德成 ) Lectures 9-10 1

Solution

1. f:Neven, f(n)=2n

2. f:NZ- f(z)=-1-z

3.

4.

5.

6.

7. 36

otherwise 1)-af(x

0 xif )(xf

otherwise 1)-aaf(x

0 xif )(xf

odd is when

2

1

even is when 2)(

xx

xx

xf

even is when 1

odd is when )(

xx

xxxf

odd is when 1

even is when )(

xx

xxxf