Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Discrete Structure Li Tak Sing( 李德成 ) Lectures 16,17,18,19 1."— Presentation transcript:

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

2 From grammar to inductive definition Consider the following grammar:  S  |aB  B  b|bB Basis: , ab  L(G) Induction: if ay  L(G), then aby  L(G). 2

3 Constructing grammars 1.{a n |n  N} S  |aS 2.{a n b n |n  N} S  |aSb 3.{(ab) n |n  N} S  |abS 3

4 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 M  N starts with the two productions: S  A|B  Product Rule: the language MN starts with the productions: S  AB  Closure Rule: The language M* starts with the productions: S  AS|  4

5 Examples Write the grammar for each of the following languages. 1.{a n bc m |m,n  N} 2.{ , a, ab, aab, aaab,....} 3.{a, ba, bba, bbba,....} 4.{a n b n |n  N}* 5.{a n b n |n  N}  {c n |n  1} 5

6 Solution 1.S  aS|Sc|b 2.S  |aB B  |bB 3.S  bS|a 4.S  AS|  A  aAb|  5.S  A|B A  aAb|  B  c|cB 6

7 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

8 Ambiguity Consider the grammar.  E  a|b|E-E The string a-b-a has the following distinct derivations. E  E-E  a-E  a-E-E  a-b-E  a-b-a E  E-E  E-E-E  a-E-E  a-b-E  a-b-a The former one has the meaning of a-(b-a) The latter one has the meaning of (a-b)-a 8

9 Removing the ambiguity If we change the grammar as this:  E  E-T|T  T  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

10 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.S  a|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

11 Solution 1.Consider the string ababa. This can be expanded as: S  SbS  abS  abSbS  ababS  ababa, or S  SbS  SbSbS  abSbS  ababS  ababa 2.Consider the string abb S  abB  abbB  abb, or S  AB  AaB  aB  abB  abbB  abb 11

12 Solution 3.Consider the string aaa S  aS  aaS  aaa, or S  Sa  Saa  aaa 4.Consider the string aba S  aS  aSa  aba,or S  Sa  aSa  aba 5.Consider the string bb S  Ab  bb, or S  A  bA  bb 12

13 More Examples Find grammars for the following languages: 1.{a n b m c m d n | m,n  N} 2.{a n b m cd n | m,n  N} 3.{a n b m c m d| m,n  N} 4.{ab m c m d| m,n  N} 5.ab m cd m | m  N} 13

14 Solution 1.S  aSd|A A  bAc|  2.S  aSd|A A  bA|c 3.S  aS|Ad A  bAc|  4.S  aAd A  bAc|  5.S  aA A  bAd|c 14

15 Chapter 4 Properties of Binary Relations Three special properties  For a binary relation R on a set A, we have the following definitions. R is reflexive if xRx for all x  A. R is symmetric if xRy implies yRx for all x,y  A R is transitive if xRy and yRz implies xRz for all x,y,z  A 15

16 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 x  A.  R is antisymmetric if xRy and yRx implies x=y for all x,y  A. 16

17 Example R is a binary relation on N aRb if (a+b) mod 2 = 0 R is reflexive because (a+a) mod 2 =0 for all a  N R is symmetric because if aRb, then (a+b) mod 2 = 0, then (b+a) mod 2 =0, then bRa R 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

18 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

19 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

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

21 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

22 Solution 1.Not reflexive, symmetric, not transitive, irreflexive, not anti-symmetric 2.Not reflexive, not symmetric, not transitive, irreflexive, anit-symmetric 22

23 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=isParentOf  isParentOf So a isGrandparentOf b if there is c so that a isParentOf c and c isPrentOf b. 23

24 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 S  R 2.Find R  S 24

25 Solution 1.{(a,b),(a,c),(b,b),(b,c)} 2.{(a,a),(a,d),(a,b),(c,a),(c,d),(d,b)} 25

26 Representations If R is a binary relation on A, then we'll denote the composition of R with itself n times by writing R n. For example,  isGrandparentOf=isParentOf 2  isGreatGrandParentOf=isParentOf 3 26

27 Inheritance properties If R is reflexive, then R n is reflexive. If R is symmetric, then R n is symmetric. If R is transitive, then R n is transitive. 27

28 Example Let R={(x,y)  Z  Z|x+y is odd}. We want to find out R 2 and R 3. 28

29 Solution R 2 ={(x,y)|x+y is even} R 3 =R 29

30 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

31 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

32 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

33 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

34 Constructing Closures If R is a binary relation over a set A, then:  r(R)=R  R o (R o is the equality relation)  s(R)=R  R c (R c is the converse relation)  t(R)=R  R 2  R 3  R 4....  If A is finite with n elements, then t(R)= R  R 2  R 3  R 4....  R n 34

35 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

36 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

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

38 Sample equivalence relations R is over N, xRy means x+y is even R is over real number, xRy means sin(x)=sin(y) 38

39 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

40 Solution 1.R is not reflexive, is symmetric, is not transitive. 2.~ is an equivalence relation. It is reflexive because |a|=|a| for all a  Z. It is symmetric because a~b  |a|=|b|  |b|=|a|  b~a It is transitive because a~b and b~c  |a|=|b| and |b|=|c|  |a|=|c|  a~c 40

41 Intersection property of equivalence If E and F are equivalence relations on the set A, then E  F is an equivalence relation on A. 41

42 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

43 Equivalence classes Let R be an equivalence relation on a set S. If a  S, 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

44 Example of equivalence class a~b iff a+b is even a~b iff sin(a)=sin(b) 44

45 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

46 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

47 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

48 Example aRb iff a  b (mod 2) aSb iff a  b (mod 4) 48

49 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

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

51 Kruskai's algorithm for minimal spanning trees In 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

52 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

53 Spanning trees 53

54 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

55 Spanning trees 55

56 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

57 Order relations Two essential properties of order  it is transitive: if a>b and b>c then a>c  it is anitsymetric: we cannot have a<b and b<a. If a  b and b  a, then a=b. 57

58 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

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

60 A Pancake recipe We can see that there can be many valid orders:  4,5,2,1,3,6,7  1,2,3,4,5,6,7 .... 60

61 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

62 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 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

63 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

64 Example 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.  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 |. is a totally ordered set because for any m,n  N, either 2 n |2 m or 2 m |2 n. 64

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

66 Notation for Partial orders 66

67 Notation for Partial orders 67

68 Notation for Partial orders 68

69 Chains 69

70 Predecessors and Successors 70

71 Poset diagrams 71

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

73 Maxima, Minima, and Bounds 73

74 Maxima, Minima, and Bounds For example: has a least element  <{{a,b},{a},{b}} has two minimal elements, {a} and {b} but no least element. has two minimal elements, 2 and 5 but no least element. 74

75 Maxima, Minima, and Bounds 75

76 Maxima, Minima, and Bounds For example: has a greatest element {a,b,c} <{{a,b},{a},{b}} has a greatest element {a,b} has two maximal elements, 4 and 10. 76

77 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

78 Maxima, Minima, and Bounds 78

79 Maxima, Minima, and Bounds 79

80 Example 80

81 Example 5.The lower bounds of S. 6.The upper bounds of S. 7.The lub of S. 8.The glb of S. 81

82 Solution 82


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

Similar presentations


Ads by Google