410 likes | 428 Views
Complexity and Computability Theory I. Lecture #12 Instructor: Rina Zviel-Girshin Lea Epstein. Overview. Push-down automata - examples The pumping lemma for CFL Examples Interesting facts. L = {a i b j | j>i 0}. Construct a PDA to recognize: L = {a i b j | j>i 0}
E N D
Complexity and Computability Theory I Lecture #12 Instructor: Rina Zviel-Girshin Lea Epstein
Overview • Push-down automata - examples • The pumping lemma for CFL • Examples • Interesting facts Rina Zviel-Girshin @ASC
L = {aibj | j>i0} Construct a PDA to recognize: L = {aibj | j>i0} We will use the final states model. The basic idea: • write each a to the stack • for each b pop from the stack and check that the number of b’s greater than the number of a’s Rina Zviel-Girshin @ASC
L = {aibj | j>i0} Rina Zviel-Girshin @ASC
L = {aibj | j>i0} Construct a PDA to recognize: L = {aibj | j>i0} We will use the empty stack model. The basic idea: • write each a to the stack • for each b, pop from the stack until reaching Z and then for each b give two possibilities • to pop Z and stop • or to do nothing Rina Zviel-Girshin @ASC
Example Rina Zviel-Girshin @ASC
L = {aibicjdj |i,j0} Construct a PDA to recognize: L = {aibicjdj |i,j0} We will use the empty stack model. The basic idea: • write each a to the stack and for each b pop • write each c to the stack and for each d pop Rina Zviel-Girshin @ASC
L = {aibicjdj |i,j0} Rina Zviel-Girshin @ASC
Context-Free Languages • Languages decided by PDA called • context-free languages. • Not all languages are context-free. Rina Zviel-Girshin @ASC
Non-Context-Free Languages • Consider L={aibici |i0}. • We try to build a push-down automata that recognizes (decides) it. • If we manage to build a PDA for L then the language is context-free. • We cannot build a push-down automaton that recognizes L. Rina Zviel-Girshin @ASC
L={aibici |i0} • WHY? • We can compare a number of a-'s with b-'s or c-'s but not both. • If we compared the number of a-'s to the number of b-'s then we can't compare c-'s with any of them. • Stack (or counter) is empty. Rina Zviel-Girshin @ASC
Non-Context-Free Languages • So some languages are not CFL. • The question is which? • We will answer this question later – pumping lemma for context-free languages. Rina Zviel-Girshin @ASC
Recursive grammar • Back to grammars. • Grammar is recursive if it has: - direct recursion (AwAu) or - indirect recursion (AwBu,BzA) Rina Zviel-Girshin @ASC
Direct recursion • The following rule is called a direct recursion rule: AwAu where • w,u(VU)* and • A derives A in one step and • either w or u is not empty • Why? • Unite rule. Rina Zviel-Girshin @ASC
Indirect recursion • A grammar having the following rules is called an indirect recursion grammar: AwBu BzA where • w,u,z(VU)* • either w or u is not empty • z is not empty • A derives A in two steps Rina Zviel-Girshin @ASC
Recursive grammar • Grammar is recursive if it has direct or indirect recursion. Example: SaS | b SaA AcS Rina Zviel-Girshin @ASC
Infinite Language • If a context-free language (CFL) has an infinite number of words then its grammar is recursive one. • This means that there must be at least one production rule that is recursive (directly or indirectly). • Recursive rules are used to generate strings of arbitrary length. (infinite) Rina Zviel-Girshin @ASC
Formal proof • Proof by contradiction: • We assume that L(G) is infinite and G is a grammar with no recursive rules. • Let n be a number of rules in G. • The longest string in G can be parsed in at most n steps - (no recursion means there is no reuse of the same production rule, so after using n rules all rules are used). 1 n Rina Zviel-Girshin @ASC
Formal proof (cont) Each step should be a different rule. Now we can “play” with sequences of rules. We have n! different sequences. That means – number of strings produced by non recursive grammar is bounded by n!. The language L(G) is finite. Contradiction to the assumption L(G) is infinite. 1 n Rina Zviel-Girshin @ASC
Infinite Language Example SuAz AvAy | x where either v or y is not empty. • If we do not use the recursive rule we derived a language that consist of only one string - uxz. • If we use the recursive rule than the grammar allows us to derive infinitely many strings having the same construction (pattern). Rina Zviel-Girshin @ASC
Example (cont.) • For example w = uv2xy2z can be derived as follows: SuAzuvAyzuvvAyyz uvvxyyz • The same pattern can be used to derive all words of the form w = uvkxykz for all k0. Rina Zviel-Girshin @ASC
Pumping length • That means that every CFL has a special value called the pumping length such that all strings longer than the pumping length can be "pumped". • The string can be divided into 5 parts w=uvxyz. • The second and fourth can be pumped. w = uvkxykz for all k0 Rina Zviel-Girshin @ASC
Pumping Lemma for CFL • Let L be an infinite context-free language. • There is a positive integer p (the pumping length) such that for all strings wL with |w|p, w can be divided into five pieces w=uvxyz satisfying the following conditions: 1. |vy|>0 2. |vxy|p 3. for each i0, uvixyizL Rina Zviel-Girshin @ASC
Proof - value of p • Let G be a CFG for CFL L. • First we find out the value of p. • Let n be the maximum number of symbols in the right side of some rule in G. • So we know that in any parse tree of G a node can't have more than n children. • So if the height of the tree is h then the length of the string its generate is at most nh. Rina Zviel-Girshin @ASC
Proof - value of p • Let |V| be the number of variables in G. • We set p to be n|V|+2 . • p= n|V|+2 • Because n2 (no unite rules) then for any string of length p the parse tree requires height at least |V|+2. • Lets call this string w: |w|p . Rina Zviel-Girshin @ASC
Proof • Since G has only |V| variables that means that at least one of the production rules have to be repeated (height |V|+2). • Lets call this variable R. • For convenience later, we select R to be a variable that repeats among the lowest |V|+1 variables. Rina Zviel-Girshin @ASC
Proof We divide w into uvxyz accordingly to the parse tree. Each occurrence of R has a subtree under it. Rina Zviel-Girshin @ASC
Proof • The upper occurrence of R has a larger subtree and generates vxy. • The lower occurrence of R has a smaller subtree and generates only x. • Both subtrees are generated by the same variable R. • That means if we substitute one for the other we will still obtain valid parse trees. Rina Zviel-Girshin @ASC
That establishes condition 3 of lemma. Replacing the larger by the smaller generates the string uxz or uvixyiz where i=0. Replacing the smaller by the larger repeatedly generates the string uvixyiz at each i>0. Rina Zviel-Girshin @ASC
Proof • To prove condition 1 (|vy|>0) we must be sure that both v and y are not . • We use a grammar without union rules. • So if v and y are both we have a rule R R – unite rule. Contradiction. • So condition 1 is satisfied. Rina Zviel-Girshin @ASC
Proof • To prove condition 2 (|vxy|p) we will check the height of the subtree rooted in first R - subtree that generates vxy. • It’s height is at most |V|+2. • So it can generate a string of length at most n|V|+2. But p= n|V|+2 . • Condition 2 is satisfied. Q.E.D Rina Zviel-Girshin @ASC
Usage of lemma • We use pumping lemma to prove that a language is not context-free. • To prove that language L is context-free you have: • to build a CFG for L or • to build PDA that recognizes the words of L. Rina Zviel-Girshin @ASC
L={aibici |i0} We will use the pumping lemma to prove that L={aibici |i0} is not CFL. Proof: • We assume that L is CFL and obtain a contradiction. • Let p be the pumping length for L. • We select a string w= apbpcp . wL and |w|>p, so it can be pumped. Rina Zviel-Girshin @ASC
L={aibici |i0} • Lets divide w into five parts uvxyz such that |vy|>0 and |vxy|p. • We have two cases: 1. v and y contain only one type of alphabet symbol. So if we choose i=2 then we have uv2xy2z and it cannot contain the same number of a's, b's and c's. So wL. Rina Zviel-Girshin @ASC
L={aibici |i0} 2. v and y contain only two type of alphabet symbol. (Can't have three because condition 2 holds). So if we choose i=2 then we have uv2xy2z and it cannot contain the same number of a's, b's and c's, because we pumped only two alphabet symbols. • So wL. Rina Zviel-Girshin @ASC
L={aibici |i0} • One of these cases must occur. • In both cases we have a contradiction to lemma. • So the assumption that L is CFL must be false. • L={aibici |i0} is not CFL. Rina Zviel-Girshin @ASC
Intersection • CFL are not closed under intersection. Example: L1={anbnck |n,k>0} L2={akbncn | n,k>0} Both L1 and L2 are CFL L1L2 = {ajbjcj | j>0} -not CFL Rina Zviel-Girshin @ASC
Complement • CFL are not closed under complement. Proof by contradiction: • Lets take two CFL L1 and L2 and assume that L1' and L2‘ are CFL. • By union of L1' and L2' we obtain another CFL. (Since CFL closed under union operation). Rina Zviel-Girshin @ASC
Complement • If we take the compliment of this union we again obtain CFL. • (L1'L2')'=L1L2 - is CFL. • But we know that {anbnck |n,k>0} {akbncn | n,k>0} = {anbncn | n>0} -not CFL Rina Zviel-Girshin @ASC
Any Questions? Rina Zviel-Girshin @ASC