Discrete Structure Li Tak Sing( 李德成 ) Lectures 16,17,18,19 1.

Post on 28-Dec-2015

223 views 1 download

Tags:

Transcript of Discrete Structure Li Tak Sing( 李德成 ) Lectures 16,17,18,19 1.

Discrete Structure

Li Tak Sing(李德成 )

Lectures 16,17,18,19

1

From grammar to inductive definition

Consider the following grammar:S|aBB b|bB

Basis: , abL(G)Induction: if ay L(G), then aby L(G).

2

Constructing grammars

1. {an|nN}S|aS

2. {anbn|n N}S |aSb

3. {(ab)n|nN}S |abS

3

Combining grammars

Suppose M and N are languages whose grammars have disjoint sets of non-terminals. Suppose also that the start symbols for the grammars of M and N are A and B respectively. Then we have the following new languages and grammars: Union rule: the language MN starts with the two productions:

SA|B Product Rule: the language MN starts with the productions:

S AB Closure Rule: The language M* starts with the productions:

S AS|

4

Examples

Write the grammar for each of the following languages.1. {anbcm|m,nN}

2. {, a, ab, aab, aaab, ....}

3. {a, ba, bba, bbba, ....}

4. {anbn|n N}*

5. {anbn|n N}{cn|n1}

5

Solution

1. SaS|Sc|b

2. S |aBB|bB

3. SbS|a

4. SAS|A aAb|

5. S A|BA aAb| Bc|cB

6

Meaning and ambiguity

A grammar is said to be ambiguous if its language contains some string that has two different parse trees. This is equivalent to saying that some string has two distinct leftmost derivations or that some string has two distinct rightmost derivations.

7

Ambiguity

Consider the grammar.Ea|b|E-E

The string a-b-a has the following distinct derivations.

EE-E a-E a-E-E a-b-E a-b-aE E-E E-E-E a-E-E a-b-E a-b-aThe former one has the meaning of a-(b-

a)The latter one has the meaning of (a-b)-a 8

Removing the ambiguity

If we change the grammar as this:E E-T|TT a|b

In the above grammar, T can only be replaced by a or b. So only the right operand can be further expands to another expression that have -. So a-b-a will only be expanded to have the meaning of (a-b)-a

9

Ambiguity

Show that each of the following grammers is ambiguous. In other words, find a string that has two different parse trees (equivalently, two different leftmost derivations or two different rightmost derivations)1. Sa|SbS.

2. S abB|AB and A |Aa and B |bB.

3. S aS|Sa|a

4. S aS|Sa|b

5. S Ab|A and A b|bA

10

Solution

1. Consider the string ababa. This can be expanded as:SSbS abS abSbS ababS ababa, orS SbS SbSbS abSbS ababSababa

2. Consider the string abbS abB abbB abb, orS AB AaB aB abB abbB abb

11

Solution

3. Consider the string aaaS aS aaS aaa, orS Sa Saa aaa

4. Consider the string abaS aS aSa aba,orS Sa aSa aba

5. Consider the string bbS Ab bb, orS A bA bb

12

More Examples

Find grammars for the following languages:1. {anbmcmdn| m,n N}

2. {anbmcdn| m,n N}

3. {anbmcmd| m,n N}

4. {abmcmd| m,n N}

5. abmcdm| m N}

13

Solution

1. SaSd|AA bAc|

2. S aSd|AA bA|c

3. S aS|AdA bAc|

4. S aAdA bAc|

5. S aAA bAd|c

14

Chapter 4Properties of Binary RelationsThree special properties

For a binary relation R on a set A, we have the following definitions.R is reflexive if xRx for all xA.R is symmetric if xRy implies yRx for all x,y AR is transitive if xRy and yRz implies xRz for all x,y,z

A

15

Two opposite properties

For a binary relation R on a set A, we have the following definitions.R is irreflexive if (x,x)R for all xA.R is antisymmetric if xRy and yRx implies x=y

for all x,yA.

16

Example

R is a binary relation on NaRb if (a+b) mod 2 = 0R is reflexive because (a+a) mod 2 =0 for all a NR is symmetric because if aRb, then (a+b) mod 2 = 0, then (b+a) mod 2 =0, then bRaR is transitive, because if aRb and bRc, then (a+b) mod 2 =0 and (b+c) mod 2 =0, then (a+2b+c) mod 2 =0, then (a+c) mod 2 =0, then aRc

17

Example

Give examples of binary relations over the set {a,b,c,d} with the stated properties:1. Reflexive and not symmetric and not transitive

2. Symmetric and not reflexive and not transitive

3. transitive and not reflexive and not symmetric

18

Solution

1. {(a,a),(b,b),(c,c),(d,d),(a,b),(b,c)}

2. {(a,b),(b,a)}

3. {(a,b),(b,b)}

19

Composition of relations

If R and S are binary relations, then the composition of R and S, which we denote by SR, is the following relation: SR={(a,c)|(a,b)R and (b,c) S for some element b}

20

More examples

For each of the following binary relations state which of the three properties, reflexive, symmetric and transitive are satisfied.1. xRy iff |x-y| is odd, over the integers.

2. xRy iff x is a parent of y, over the set of people.

21

Solution

1. Not reflexive, symmetric, not transitive, irreflexive, not anti-symmetric

2. Not reflexive, not symmetric, not transitive, irreflexive, anit-symmetric

22

Grandparents

Given the isParentOf relation. So a isParentOf b represents the fact that a is the parent of b.

isGrandparentOf can then be defined in terms of isParentOf.isGrandparentOf=isParentOfisParentOf

So a isGrandparentOf b if there is c so that a isParentOf c and c isPrentOf b.

23

More examples

Given the following binary relations over {a,b,c,d}.R={(a,a),(a,c),(b,a),(b,d),(c,b)}S={(a,b),(a,c),(c,b),(d,c)}1. Find SR2. Find RS

24

Solution

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

2. {(a,a),(a,d),(a,b),(c,a),(c,d),(d,b)}

25

Representations

If R is a binary relation on A, then we'll denote the composition of R with itself n times by writing

Rn.For example,

isGrandparentOf=isParentOf2

isGreatGrandParentOf=isParentOf3

26

Inheritance properties

If R is reflexive, then Rn is reflexive.If R is symmetric, then Rn is symmetric.If R is transitive, then Rn is transitive.

27

Example

Let R={(x,y)ZZ|x+y is odd}. We want to find out R2 and R3.

28

Solution

R2 ={(x,y)|x+y is even}R3 =R

29

Closures

If R is a binary relation and p is some property, then the p closure of R is the smallest binary relation containing R that satisfies property p.

30

Reflexive closure

A reflexive closure of R is the smallest reflexive relation that contains R.

A reflexive closure of R is denoted as r(R)R is a relation over {a,b,c} and R={(a,b),

(b,c)}Then, r(R)={(a,a),(b,b),(c,c),(a,b),(b,c)}

31

Symmetric closure

A symmetric closure of R is the smallest symmetric relation that contains R.

A symmetric closure of R is denoted as s(R)

R={(a,b),(b,c)}, s(R)={(a,b),(b,a),(b,c),(c,b)}

32

Transitive closure

A transitive closure of R is the smallest transitive relation that contains R. It is denoted as t(R).

R= {(a,b),(b,c)}, then t(R)= {(a,b),(b,c),(a,c)}

33

Constructing Closures

If R is a binary relation over a set A, then:r(R)=RRo (Ro is the equality relation)s(R)=R Rc (Rc is the converse relation)t(R)=R R2 R3 R4....If A is finite with n elements, then t(R)= R R2

R3 R4.... Rn

34

Example

Given the set A={a,b,c,d}. Draw a directed graph to represent the indicated closure for each of the following binary relations over A.1. r(R), where R={(a,d)}

2. s(R) where R={(a,b), (c,d)}

3. t(R) where R={(a,b),(d,a),(d,c),(c,b)}

35

Solution

1. r(R)={(a,a),(b,b),(c,c),(d,d),(a,d)}

2. s(R)={(a,b),(b,a),(c,d),(d,c)}1. t(R) ={(a,b),(d,a),(d,c),(c,b),(d,b)}

36

Equivalence relations

Any binary relation that is reflexive, symmetric, and transitive is called an equivalence relation.

37

Sample equivalence relations

R is over N, xRy means x+y is evenR is over real number, xRy means

sin(x)=sin(y)

38

Examples

1. Let R be defined on N by xRy iff |x-y| is odd. Show that R is not an equivalance relation on N.

2. Given the relation over the integers defined by a~b iff |a|=|b|, either prove that ~ is an equivalence relation or prove that ~ not an equivalence relation.

39

Solution

1. R is not reflexive, is symmetric, is not transitive.

2. ~ is an equivalence relation. It is reflexive because |a|=|a| for all aZ.It is symmetric because a~b|a|=|b| |b|=|a| b~aIt is transitive because a~b and b~c |a|=|b| and |b|=|c| |a|=|c|a~c

40

Intersection property of equivalence

If E and F are equivalence relations on the set A, then EF is an equivalence relation on A.

41

Kernel relations

If f is a function with domain A, then the relation ~ defined by x~y iff f(x)=f(y)is an equivalence relation on A, and it is called the kernel relation of f.

42

Equivalence classes

Let R be an equivalence relation on a set S. If aS, then the equivalence class of a, denoted by [a], is the subset of S consisting of all elements that are equivalent to a. In other words, we have [a]={x S | xRa}

43

Example of equivalence class

a~b iff a+b is evena~b iff sin(a)=sin(b)

44

Property of equivalences

Let S be a set with an equivalence relation R. If a, b S, then either [a]=[b] or [a][b]=.

Proof. If [a][b], and [a][b] , there is an element c so that c[a] and c[b]. So we have aRc and cRb, therefore aRb. Now, for any x [a] , xRa. Since aRb, therefore we have xRb which implies that x [b]. Therefore, [a][b]. Similarly, we have [b][a].

45

Partitions

By a partition of a set we mean a collection of nonempty subsets that are disjoint from each other and whose union is the whole set.

If R is an equivalence relation on the set S, then the equivalence classes form a partition of S. Conversely, if P is a partition of a set S, then there is an equivalence relation on S whose equivalence classes are sets of P.

46

Refinement of a partition

Suppose that P and Q are two partitions of a set S. If each set of P is a subset of a set in Q, then P is a refinement of Q. The finest of all partitions on S is the collection of singleton sets. The coarsest of all partitions of S is the set S itself.

47

Example

aRb iff ab (mod 2)aSb iff ab (mod 4)

48

Example

Given the following set of words. {rot, tot, root, toot, roto, toto, too, to ,otto}.1. Let f be the function that maps word to its set

of letters. For kernel relation of f, describe the equivalence classes.

2. Let f be the function that maps a word to its bag of letters. For the kernel relation of f, describe the equivalence classes.

49

Solution

1. [rot]={rot,root,roto}[to]={to,toot,toto,too,to,otto}

50

Kruskai's algorithm for minimal spanning treesIn the spanning tree problems, we can

define a relation R so that aRb if there is a path between a and b. The algorithm is:

51

Kruskal's Algorithm

Sort the edges of the graph by weight, and let L be the sorted list.

Let T be the minimal spanning tree and initialize T:=.For each vertex v of the graph, create the equivalence

class [v]={v}while there are 2 or more equivalence classes do

Let {a,b} be the edge at the head of L; L:=tail(L); if [a][b] then T:=T{{a,b}}; Replace the equivalence classes [a] and [b] by [a] [b] fi

od

52

Spanning trees

ab

cd

e

f

22

12

1

2

2

53

Solution

54

L [v] T

ad,bc,cf,ea,ab,ed,dc,ef {a},{b},{c},{d},{e},{f}

bc,cf,ea,ab,ed,dc,ef {a,d},{b},{c},{e},{f} ad

cf,ea,ab,ed,dc,ef {a,d},{b,c},{e},{f} ad,bc

ea,ad,ed,dc,ef {a,d},{b,c,f},{e} ad,bc,cf

ad,ed,dc,ef {a,d,e},{b,c,f} ad,bc,cf,ea

ed,dc,ef {a,d,e},{b,c,f} ad,bc,cf,ea

dc,ef {a,d,e},{b,c,f} ad,bc,cf,ea

ef {a,d,e,b,c,f} ad,bc,cf,ea,dc

Spanning trees

a

b

c

d

e

f

1

23

2

1

1

1

2

55

Solution

L {v} T

ab,bf,bc,dc,ea,ac,ed,ef,fd {a},{b},{c},{d},{e},{f}

bf,bc,dc,ea,ac,ed,ef,fd {b,f},{a},{c},{d},{e} ab

bc,dc,ea,ac,ed,ef,fd {b,c,f},{a},{d},{e} ab,bf

dc,ea,ac,ed,ef,fd {b,c,f},{a},{d},{e} ab,bf

ea,ac,ed,ef,fd {b,c,f,d},{a},{e} ab,bf,dc

ac,ed,ef,fd {b,c,f,d},{a,e} ab,bf,dc,ea

ed,ef,fd {b,c,f,d,a,e} ab,bf,dc,ea,ac

56

Order relations

Two essential properties of orderit is transitive: if a>b and b>c then a>cit is anitsymetric: we cannot have a<b and b<a.

If ab and b a, then a=b.

57

A Pancake recipe

Suppose we have the following recipe for making pancakes.

1. Mix the dry ingredients (flour, sugar, baking powder) in a bowl.

2. Mix the wet ingredients (milk, eggs) in a bowl.3. Mix the wet and dry ingredients together.4. Oil the pan.5. Heat the pan.6. Make a test pancake and throw it away.7. Make pancakes.

58

A Pancake recipe

We see that some steps are need to be done before others. We can show this order as a digraph:

1

2 3 6 7

4 5

59

A Pancake recipe

We can see that there can be many valid orders:4,5,2,1,3,6,71,2,3,4,5,6,7....

60

A Pancake recipe

We can define a relation R as:xRy means that step x must be done before step y

R is irreflexive because a step cannot be done before itself.

R is antisymetric because when a step has to be done before another, it cannot done the other way round.

R is transitive.

61

Partial order

A binary relation is called a partial order if it is anitsysmetric and transitive. The set over which a partial order is defined is called a partially ordered set- or poset for short. If we want to emphasize the fact that R is the partial order that makes S a poset, we'll write <S,R> and call it a poset.

It is called partial order because not all elements are related.

For example, steps 1 and 2 of the pancake example are not related, i.e., there is no order of steps 1,2.

62

Total order

Suppose R is a binary relation on a set S and x, y in S. We say that x and y are comparable if either xRy or yRx. In other words, elements that are related are comparable. If every pair of distinct elements in a partial order are comparable, then the order is called a total order (also called a linear order). If R is a total order on the set S, then we also say that S is a totally ordered set or linearly ordered set.

63

Example

<N,|> is a poset because | is transitive: a|b and b|c implies a|c| is antisymmetric: if a|b, then either a=b or b

does not divides b.<N,|> is not total ordered because there are

some elements in N that is not related under |. For example, 3 and 5 are not related under |.

<{2n|nN},|> is a totally ordered set because for any m,n N, either 2n|2m or 2m|2n.

64

reflexive partial order and irreflexive partial orderIf a partial order is reflexive, we call it

reflexive partial order.If a partial order is irreflexive, we call it

irreflexive partial order.<N,≤> is reflexive partial order.<N,<> is irreflexive partial order.

65

Notation for Partial orders

When talking about partial orders, we'll often use the symbols and to stand for an irreflexive partial order and a reflexive partial order respectively. We can read as "a is less than b", and as "a is less than or equal to b".

66

Notation for Partial orders

The two symbols can be defined in terms of each other. For example, if is a poset, then we can define the relation in terms of by writing:

In other words, is the reflexive closure of . So always means or .

67

Notation for Partial orders

Similarly, if is a poset, then we can define the relation in terms of by writing:

68

Chains

A set of elements in a poset is called a chain if all the elements are comparable-linked-to each other. For example, any totally ordered set is itself a chain. A sequence of elements , ,... in a poset is said to be descending chain if ...

69

Predecessors and Successors

If , then we say that is a predecessor of , or y is a successor of .

Suppose that and there is no elements between and . In other words, suppose we have the following situation:

When this is the case, we say that is an immediate predecessor of , or is an immediate successor of .

70

Poset diagrams

A poset can be represented by a special graph called a poset diagram. Whenever and is immediate predecessor of , then place an edge (, ) in the poset diagram with x at a lower level than y.

A poset diagram can often help us observe certain properties of a poset.

71

Example

Draw a poset diagram of the partial order defined by the subset relation on power({a,b,c}).

72

Maxima, Minima, and Bounds

Suppose that S is any nonempty subset of a poset P. An element xS is called a minimal element of S if x has no predecessors in S. An element x S is called the lease element of S if x is minimal and for all y S

73

Maxima, Minima, and Bounds

For example:

<power({a,b,c}), > has a least element <{{a,b},{a},{b}} has two minimal elements, {a} and {b} but no least element.

<{2,4,5,10}, |> has two minimal elements, 2 and 5 but no least element.

74

Maxima, Minima, and Bounds

An element xS is called a maximal element of S if x has no successors in S. An element x S is called the greatest element of S if x is maximal and for all y S.

75

Maxima, Minima, and Bounds

For example:

<power({a,b,c}), > has a greatest element {a,b,c}

<{{a,b},{a},{b}} has a greatest element {a,b}

<{2,4,5,10}, |> has two maximal elements, 4 and 10.

76

Maxima, Minima, and Bounds

Some sets may not have any minimal elements, yet still be bounded below by some element. For example, the set of positive rational numbers has no least element yet is bounded below by the number 0.

77

Maxima, Minima, and Bounds

If S is a nonempty subset of a poset P, an element xP is called a lower bound of S if for all y S. An element xP is called the greatest lower bound (or glb) of S if x is a lower bound and for all lower bounds z of S.

78

Maxima, Minima, and Bounds

If S is a nonempty subset of a poset P, an element xP is called an upper bound of S if for all y S. An element xP is called the lowest upper bound (or lub) of S if x is a upper bound and for all upper bounds z of S.

79

Example

Given the set P=ZZ and the partial order on P defined by

iff

Let S=NN. Describe each of the following.

1. The minimal elements of S.

2. The least element of S.

3. The maximal elements of S.

4. The greatest element of S.80

Example

5. The lower bounds of S.

6. The upper bounds of S.

7. The lub of S.

8. The glb of S.

81

Solution

1. No least element.

2. no maximal elements

3. No greatest element.

4. No upper bounds

5. no lub

6. no glb82