47
Discrete Structures Li Tak Sing( 李李李 ) Lecture note 2 1

Discrete Structures Li Tak Sing( 李德成 ) Lecture note 2 1

Embed Size (px)

Citation preview

Page 1: Discrete Structures Li Tak Sing( 李德成 ) Lecture note 2 1

Discrete Structures

Li Tak Sing(李德成 )

Lecture note 2

1

Page 2: Discrete Structures Li Tak Sing( 李德成 ) Lecture note 2 1

Prove that power(AB)=power(A)power(B)

2

Page 3: Discrete Structures Li Tak Sing( 李德成 ) Lecture note 2 1

Proof

xpower(A)power(B) xpower(A) and xpower(B) xA and xB xABxpower(AB)

Therefore power(AB)=power(A)power(B)

3

Page 4: Discrete Structures Li Tak Sing( 李德成 ) Lecture note 2 1

Counting finite sets

The size of a set S is called its cardinality and is denoted as |S|

||=0Counting a union set:

|AB|=|A|+|B|-|AB||ABC|=|A|+|B|+|C|-|AB|-|BC|-|CA|

+|AB C||A-B|=|A|-|AB|

4

Page 5: Discrete Structures Li Tak Sing( 李德成 ) Lecture note 2 1

Counting example

In a class, 15 students study economics, 20 students study history, 13 students study literature, 5 students study both both economics and history, 7 students study both history and literature, 4 students study both literature and economics, no students study all three courses. 4 students study none of these courses. What is the number of students in the class.

5

Page 6: Discrete Structures Li Tak Sing( 李德成 ) Lecture note 2 1

Counting example(solution)

|E|=15|H|=20|L|=13|EH|=5|H L|=7|L E|=4|H L E|=0

6

Page 7: Discrete Structures Li Tak Sing( 李德成 ) Lecture note 2 1

Counting example(solution)

Therefore|HLE|=|H|+|L|+|E|-|HL|-|LE|-|EH|+ |HLE| =15+20+13-5-7-4+0 =32

Therefore the number of students in the class is 32+4=36.

7

Page 8: Discrete Structures Li Tak Sing( 李德成 ) Lecture note 2 1

More examples

Suppose a highway survey crew noticed the following information about 500 vehicles: In 100 vehicles the driver was smoking, in 200 vehicles the driver was talking to a passenger, and in 300 vehicles the driver was tuning the radio. Further, in 50 vehicles the driver was smoking and talking, in 40 vehicles the driver was smoking and tuning the radio, and in 30 vehicles the driver was talking and tuning the radio, in 10 vehicles the driver was smoking, talking and tuning the radio. Find the number of vechicles in which the driver was not talking, smoking nor tuning the radio.

8

Page 9: Discrete Structures Li Tak Sing( 李德成 ) Lecture note 2 1

Solution

|S|=100, |T|=200, |R|=300,|ST|=50, |SR|=40, |RT|=30|S T R|=10

|ST R|=100+200+300-50-40-30+10=490

Therefore |(ST R)'|=500-490=10

9

Page 10: Discrete Structures Li Tak Sing( 李德成 ) Lecture note 2 1

Ordered structures

A tuple is a number of elements with a distinct order. An n-tuple contains n elements and is denoted as (x1, x2,...,xn) where x1, x2, ..., xn are the n elements of the tuple. For example, (3,2) is a 2-tuple, while () is a 0-tuple.

2-tuples are also called ordered pairs.

10

Page 11: Discrete Structures Li Tak Sing( 李德成 ) Lecture note 2 1

Two characteristics of tuples

There may be repeated occurrences of elements.

There is an order or arrangement of the elements.

11

Page 12: Discrete Structures Li Tak Sing( 李德成 ) Lecture note 2 1

Cartesian Product of Sets

If A, B are sets, then the Cartesian product of A and B, which is denoted as AB, is the set of all ordered pairs (a,b) such that aA and bB.

AB={(a,b)| aA and bB}For example, if A={1,2}, B={a,b,c}, then

AB={(1,a),(1,b),(1,c),(2,a),(2,b),(2,c)}|AB|= |A||B|

12

Page 13: Discrete Structures Li Tak Sing( 李德成 ) Lecture note 2 1

Cartesian products of A1, A2, ...,An

A1A2 .... An={(a1,a2,..,an)|xiAi}

|A1A2 .... An|= |A1| |A2|....|An|

If all the sets are identical, we can write it as An.

If A={1,a}, thenA0={()}, A1={(1),(a)}, A2={(1,1),(1,a),(a,1),(a,a)}

13

Page 14: Discrete Structures Li Tak Sing( 李德成 ) Lecture note 2 1

Examples

1. Write down all possible 3-tuples over the set {a,{a}}.

2. Let A={1,2} and B={2,3}. Compute each of the following:1. AB

2. A2

3. A2 (AB)

14

Page 15: Discrete Structures Li Tak Sing( 李德成 ) Lecture note 2 1

Solution

1. {(a,a,a),(a,a,{a}),(a,{a},a),(a,{a},{a}), ({a},a,a),({a},,a,{a}),({a},,{a},a),({a},{a},{a})}

2. 1. {(1,2),(1,3),(2,2),(2,3)}2. {(1,1),(1,2),(2,1),(2,2)}3. {(1,2),(2,2)}

15

Page 16: Discrete Structures Li Tak Sing( 李德成 ) Lecture note 2 1

Arrays, matrices, records

A one dimensional array of size n is an n-tuple.

A two dimensional array is a matrix. A matrix of size mn is an m-tuple of n-tuples.

16

Page 17: Discrete Structures Li Tak Sing( 李德成 ) Lecture note 2 1

323

231a

This 23 matrix can be represented by an 2-tuple of 3-tuples:

a=((1,3,-2),(3,2,3))

17

Page 18: Discrete Structures Li Tak Sing( 李德成 ) Lecture note 2 1

Lists

A list is a finite ordered sequence of zero or more elements that can be repeated.

What is the difference between a list and a tuple? For a tuple, you can access any member of it directly. For a list, you have access to two entity: head which is the first element and a tail which is a list that contains the remaining elements.

18

Page 19: Discrete Structures Li Tak Sing( 李德成 ) Lecture note 2 1

Lists

<> represents the empty list.<1,2,2> represents a list with three

elements.<1,<1,2,3>,2> represents a list with three

elements. The second element is a list.

19

Page 20: Discrete Structures Li Tak Sing( 李德成 ) Lecture note 2 1

Head, tail

head(L) represents the head of the list L. head(<1,2,3>)=1head(<>) is an error

tail(L) represents the tail of L.tail(<1,2,3>)=<2,3> (note that it is a list)tail(<1,2>)=<2>tail(<1>)=<>tail(<>) is an error

20

Page 21: Discrete Structures Li Tak Sing( 李德成 ) Lecture note 2 1

cons

cons(h,L) represent the concatenation of h and L to form another list.

cons(1,<2,3>)=<1,2,3>cons(<1>,<2,3>)=<<1>,23>The cons operation can also be written

as ::cons(x,y)=x::y1::<2,3>=cons(1,<2,3>)=<1,2,3>

21

Page 22: Discrete Structures Li Tak Sing( 李德成 ) Lecture note 2 1

Computer representation of lists

Each element of a list is allocated a memory block so that the block contains two things, the value of the element and the address to the next element.

22

Page 23: Discrete Structures Li Tak Sing( 李德成 ) Lecture note 2 1

Memory representation of a list

L1 2 2

L=<1,2,2>

1 2 2

M3

M=cons(3,L)=<3,1,2,2>

23

Page 24: Discrete Structures Li Tak Sing( 李德成 ) Lecture note 2 1

Examples

1. Write down all possible lists of length 2 or less over the set A={a,b}.

2. Find the head and tail of each of the list.1. <>

2. <a>

3. <<a>>

4. <a,<b,c>>

5. <<a,b>,<c,d>>

24

Page 25: Discrete Structures Li Tak Sing( 李德成 ) Lecture note 2 1

Solution

1. <>, <a>, <b>, <a,a>, <a,b>, <b,a>, <b,b>

2. 1. head(<>)=error, tail(<>)=error2. head(<a>)=a, tail(<a>)=<>3. head(<<a>>)=<a>, tail(<<a>>)=<>4. head (<a,<b,c>>)=a, tail(<a,<b,c>>)=<<b,c>>5. head(<<a,b>,<b,c>>)=<a,b> tail(<<a,b>,<b,c>>)=<<b,c>>

25

Page 26: Discrete Structures Li Tak Sing( 李德成 ) Lecture note 2 1

Strings

A string is a finite ordered sequence of zero or more elements that are placed next to each other. The individual elements that make up a string are taken from a finite set called an alphabet.

Let the alphabet be A={a,b}. A string over {a,b} can be b, aa, bba, etc.

26

Page 27: Discrete Structures Li Tak Sing( 李德成 ) Lecture note 2 1

Empty string is a string with no elements and is denoted as .

The length of a string s is denoted as |s|.Two string s and t can be concatenated as

st. For example if s is aab and t is bba, then st is aabbba.

For a string s, ss can be written as s2, and the concatenation of n s is sn.

s0=.27

Page 28: Discrete Structures Li Tak Sing( 李德成 ) Lecture note 2 1

Languages

A language is a set of strings. If A is an alphabet, then a language over A is a set of strings over A. The set of all strings over A is denoted by A*, so a language over A is a subset of A*.

For example, if A={a,b}, the following are languages over A:{ab}, {aab, bab}, {}

28

Page 29: Discrete Structures Li Tak Sing( 李德成 ) Lecture note 2 1

Example of languages

{an|nN}{ambn|m,nN}

29

Page 30: Discrete Structures Li Tak Sing( 李德成 ) Lecture note 2 1

Products of Languages

The product of languages L and M is the language LM={st| sL and tM}

L={13,44}, M={a, aa}, then LM={13a,13aa,44a,44aa}

Ln={s1s2...sn|siL}if L={a,ab}, then L0={}

L1={a,ab}L2={aa, aab, aba, abab}

30

Page 31: Discrete Structures Li Tak Sing( 李德成 ) Lecture note 2 1

If L is a language, then L0L1....Ln..... is the closure of L and is denoted as L*. An element of L* is either or a concatenation of strings from L.

if L={a, ab}, then the followings are elements of L*:aaaabaabaaa, abababaaa, ....

Closures

31

Page 32: Discrete Structures Li Tak Sing( 李德成 ) Lecture note 2 1

Positive closures

L+= L1....Ln..... is the positive closure of L.

L*={}L+ but L+ may not equal to L*-{} because L+ may itself contain , in this case L*=L+.

If L={,a}, then, L*=L+={,a,a2,a3,...}

32

Page 33: Discrete Structures Li Tak Sing( 李德成 ) Lecture note 2 1

Examples

Let L={,a,bb} and M={ba,aa}. Evaluate each of the following language expresssion:1. LM

2. ML

3. L0

4. L1

5. L2

33

Page 34: Discrete Structures Li Tak Sing( 李德成 ) Lecture note 2 1

Solution

1. {ba,aa,aba,aaa,bbba,bbaa}

2. {ba,baa,babb,aa,aaa,aabb}

3. {}

4. {,a,bb}

5. {,a,bb,aa,abb,bba,bbbb}

34

Page 35: Discrete Structures Li Tak Sing( 李德成 ) Lecture note 2 1

Use your wits to solve each of the following language equations for the unknown language.1. {,a,ab}L={b,ab,ba,aba,abb,abba}

2. L{a,b}={a,baa,b,bab}

3. {a,aa,ab}L={ab,aab,abb,aa,aaa,aba}

4. L{,a}={,a,b,ab,ba,aba}

35

Page 36: Discrete Structures Li Tak Sing( 李德成 ) Lecture note 2 1

Solution

1. {b,ba}

2. {,ba}

3. {b,a}

4. {,b,ab}

36

Page 37: Discrete Structures Li Tak Sing( 李德成 ) Lecture note 2 1

Example

Let L and M be two languages. For each of the following languages describe the general structure of a string x by writing it as a concatenation of strings that are in either L or M.1. LML

2. LM*

3. (LM)*

4. (LM)*37

Page 38: Discrete Structures Li Tak Sing( 李德成 ) Lecture note 2 1

Solution

1. LML={abc|a,cL and b M}

2. LM*={ab1b2.... bn| a L and bi M where 1in and 0 n}

3. (LM)*={b1b2.... bn| bi LM where 1in and 0 n}

4. (LM)*={a1b1a2b2.... anbn | ai L, bi M where 1in and 0 n}

38

Page 39: Discrete Structures Li Tak Sing( 李德成 ) Lecture note 2 1

Example

Try to describe each of the following languages in some way.1. {a,b}*{b,c}*

2. {a,b}*-{b}*

3. {a,b,c}*-{a}*

39

Page 40: Discrete Structures Li Tak Sing( 李德成 ) Lecture note 2 1

Solution

1. {b}*

2. All strings that contains only a or b and there is at least one a in the string.

3. All strings that contains only a, b or c and there is at least one b or c in the string.

40

Page 41: Discrete Structures Li Tak Sing( 李德成 ) Lecture note 2 1

Relations

If R is a subset of A1A2...An, then, R is said to be an n-ary relation on A1A2...An.

For example, assume A1={tom, peter, kelvin}, A2={mary, jessy}. Now, tom, peter are husbands of mary and jessy respectively. If we define R to a relation that represents marriage, then tom and mary are related this way.

41

Page 42: Discrete Structures Li Tak Sing( 李德成 ) Lecture note 2 1

So R={(tom,mary),(peter,jessy)}. Note that R A1A2.

Let A={1,4}, B={3,9}. Let R represents the relation that an integer from A is less than an integer from B. So R contains the following elements: {(1,3),(1,9),(4,9)}. We can also write R as:R={(x,y)|xA and yB and x<y}

42

Page 43: Discrete Structures Li Tak Sing( 李德成 ) Lecture note 2 1

If R is a relation and (x1,x2,...,xn)R, we would write: R(x1,x2,...,xn).

For a binary relation, R(x1,x2) is usually written as x1

Rx2.

43

Page 44: Discrete Structures Li Tak Sing( 李德成 ) Lecture note 2 1

An example. Assume that A={1,2,..,10}. Find the relation R so that:R={(x,y)| x,yA and x2=y}

44

Page 45: Discrete Structures Li Tak Sing( 李德成 ) Lecture note 2 1

Solution

R={(1,1),(2,4),(3,9)}

45

Page 46: Discrete Structures Li Tak Sing( 李德成 ) Lecture note 2 1

Examples

Represent each relation as a set by listing each individual tuple.1. {d| dN and d>0 and d divides 12}

2. {(d,n)| d,n {2,3,4,5,6} and d divides n}

3. {(x,y,z)|x=y+z, where x,y,z {1,2,3}}

46

Page 47: Discrete Structures Li Tak Sing( 李德成 ) Lecture note 2 1

Solution

1. {1,2,3,4,6,12}

2. {(2,2),(2,4),(2,6),(3,3),(3,6),(4,4),(5,5),(6,6)}

3. {(2,1,1),(3,1,2),(3,2,1)}

47