660 likes | 800 Views
Discrete Structure. Li Tak Sing( 李德成 ). 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} S |aSb {(ab) n |nN} S |abS.
E N D
Discrete Structure Li Tak Sing(李德成)
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}
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
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}
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
Composition of relations • If R and S are binary relations, then the composition of R and S, which we denote by RS, is the following relation:RS={(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. • xRy iff |x-y| is odd, over the integers. • xRy iff x is a parent of y, over the set of people.
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 RS • Find SR
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.
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)}
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 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.
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~b iff a+b is even • a~b iff sin(a)=sin(b) • a~b iff a and b are studying the same course
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 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.
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:
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
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 ab and b a, then a=b.
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.
A Pancake recipe • We see that some steps are need to be done before others. We can show this order as a digraph:
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 • ....
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.
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.
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.