1 / 66

Discrete Structure

Discrete Structure. Li Tak Sing( 李德成 ). 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). Constructing grammars. {a n |n N} S|aS {a n b n |n N} S |aSb {(ab) n |nN} S |abS.

peigi
Download Presentation

Discrete Structure

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Discrete Structure Li Tak Sing(李德成)

  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).

  3. Constructing grammars • {an|nN} • S|aS • {anbn|n N} • S |aSb • {(ab)n|nN} • S |abS

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

  5. Examples • Write the grammar for each of the following languages. • {anbcm|m,nN} • {, a, ab, aab, aaab, ....} • {a, ba, bba, bbba, ....} • {anbn|n N}* • {anbn|n N}{cn|n1}

  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. • 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. 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. 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) • Sa|SbS. • S abB|AB and A |Aa and B |bB. • S aS|Sa|a • S aS|Sa|b • S Ab|A and A b|bA

  10. More Examples • Find grammars for the following languages: • {anbmcmdn| m,n N} • {anbmcdn| m,n N} • {anbmcmd| m,n N} • {abmcmd| m,n N} • abmcdm| m N}

  11. Chapter 4Properties 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

  12. 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.

  13. 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

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

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

  16. More examples • For each of the following binary relations state which of the three properties, reflexive, symmetric and transitive are satisfied. • xRy iff |x-y| is odd, over the integers. • xRy iff x is a parent of y, over the set of people.

  17. 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.

  18. 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)} • Find RS • Find SR

  19. 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

  20. 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.

  21. Example • Let R={(x,y)ZZ|x+y is odd}. We want to find out R2 and R3.

  22. 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.

  23. 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)}

  24. 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)}

  25. 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)}

  26. Constructing Closures • If R is a binary relation over a set A, then: • r(R)=RRo (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

  27. 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. • r(R), where R={(a,d)} • s(R) where R={(a,b), (c,d)} • t(R) where R={(a,b),(d,a),(d,c),(c,b)}

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

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

  30. Examples • Let R be defined on N by xRy iff |x-y| is odd. Show that R is not an equivalance relation on N. • 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.

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

  32. 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.

  33. 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}

  34. Example of equivalence class • a~b iff a+b is even • a~b iff sin(a)=sin(b) • a~b iff a and b are studying the same course

  35. 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].

  36. 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.

  37. 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.

  38. Example • aRb iff ab (mod 2) • aSb iff ab (mod 4)

  39. Example • Given the following set of words. {rot, tot, root, toot, roto, toto, too, to otto}. • Let f be the funtion that maps word to its set of letters. For kernel relation of f, describe the equivalence classes. • Let f be the function that maps a word to its bag of letters. For the kernel relation of f, describe the equivalence classes.

  40. 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:

  41. 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

  42. Spanning trees

  43. Spanning trees

  44. 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.

  45. A Pancake recipe • Suppose we have the following recipe for making pancakes. • Mix the dry ingredients (flour, sugar, baking powder) in a bowl. • Mix the wet ingredients (milk, eggs) in a bowl. • Mix the wet and dry ingredients together. • Oil the pan. • Heat the pan. • Make a test pancake and throw it away. • Make pancakes.

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

  47. 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 • ....

  48. 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.

  49. 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 or 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.

  50. 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.

More Related