820 likes | 984 Views
Discrete Structure. Li Tak Sing( 李德成 ) Lectures 16,17,18,19. From grammar to inductive definition. Consider the following grammar: S | aB B b|bB Basis: , abL (G) Induction: if ay L(G), then aby L(G). Constructing grammars. { a n |n N } S| aS { a n b n |n N}
E N D
Discrete Structure Li Tak Sing(李德成) Lectures 16,17,18,19
From grammar to inductive definition • Consider the following grammar: • S|aB • B b|bB • Basis: , abL(G) • Induction: if ay L(G), then aby L(G).
Constructing grammars • {an|nN} S|aS • {anbn|n N} S |aSb • {(ab)n|nN} S |abS
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|
Examples • Write the grammar for each of the following languages. • {anbcm|m,nN} • {, a, ab, aab, aaab, ....} • {a, ba, bba, bbba, ....} • {anbn|n N}* • {anbn|n N}{cn|n1}
Solution • SaS|Sc|b • S |aBB|bB • SbS|a • SAS|A aAb| • S A|BA aAb| Bc|cB
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.
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-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
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
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) • Sa|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
Solution • Consider the string ababa. This can be expanded as:SSbS abS abSbS ababSababa, orS SbS SbSbS abSbS ababSababa • Consider the string abbS abB abbB abb, orS AB AaB aB abB abbB abb
Solution • Consider the string aaaS aS aaS aaa, orS Sa Saa aaa • Consider the string abaS aS aSa aba,orS Sa aSa aba • Consider the string bbS Ab bb, orS A bA bb
More Examples • Find grammars for the following languages: • {anbmcmdn| m,nN} • {anbmcdn| m,nN} • {anbmcmd| m,nN} • {abmcmd| m,nN} • abmcdm| m N}
Solution • SaSd|AA bAc| • S aSd|AA bA|c • S aS|AdA bAc| • S aAdA bAc| • S aAA bAd|c
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 xA. • 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
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.
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
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
Solution • {(a,a),(b,b),(c,c),(d,d),(a,b),(b,c)} • {(a,b),(b,a)} • {(a,b),(b,b)}
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}
More examples • For each of the following binary relations state which of the three properties, reflexive, symmetric and transitive are satisfied. • xRyiff |x-y| is odd, over the integers. • xRyiff x is a parent of y, over the set of people.
Solution • Not reflexive, symmetric, not transitive, irreflexive, not anti-symmetric • Not reflexive, not symmetric, not transitive, irreflexive, anit-symmetric
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.
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 SR • Find RS
Solution • {(a,b),(a,c),(b,b),(b,c)} • {(a,a),(a,d),(a,b),(c,a),(c,d),(d,b)}
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
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.
Example • Let R={(x,y)ZZ|x+y is odd}. We want to find out R2 and R3.
Solution • R2={(x,y)|x+y is even} • R3=R
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.
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)}
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)}
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)}
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
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)}
Solution • r(R)={(a,a),(b,b),(c,c),(d,d),(a,d)} • s(R)={(a,b),(b,a),(c,d),(d,c)} • t(R) ={(a,b),(d,a),(d,c),(c,b),(d,b)}
Equivalence relations • Any binary relation that is reflexive, symmetric, and transitive is called an equivalence relation.
Sample equivalence relations • R is over N, xRy means x+y is even • R is over real number, xRy means sin(x)=sin(y)
Examples • Let R be defined on N by xRyiff |x-y| is odd. Show that R is not an equivalance relation on N. • Given the relation over the integers defined by a~biff |a|=|b|, either prove that ~ is an equivalence relation or prove that ~ not an equivalence relation.
Solution • R is not reflexive, is symmetric, is not transitive. • ~ 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
Intersection property of equivalence • If E and F are equivalence relations on the set A, then EF is an equivalence relation on A.
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.
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}
Example of equivalence class • a~biffa+b is even • a~biff sin(a)=sin(b)
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].
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.
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.
Example • aRb iff ab (mod 2) • aSb iff ab (mod 4)
Example • Given the following set of words. {rot, tot, root, toot, roto, toto, too, to ,otto}. • Let f be the function 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.
Solution • [rot]={rot,root,roto}[to]={to,toot,toto,too,to,otto}