Complexity and Computability Theory I Lecture #12 Instructor: Rina Zviel-Girshin Lea Epstein
Rina Overview Push-down automata - examples The pumping lemma for CFL Examples Interesting facts
Rina L = {a i b j | j>i 0} Construct a PDA to recognize: L = {a i b j | j>i 0} 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 L = {a i b j | j>i 0}
Rina L = {a i b j | j>i 0} Construct a PDA to recognize: L = {a i b j | j>i 0} 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 Example
Rina L = {a i b i c j d j |i,j 0} Construct a PDA to recognize: L = {a i b i c j d j |i,j 0} 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 L = {a i b i c j d j |i,j 0}
Rina Context-Free Languages Languages decided by PDA called –context-free languages. Not all languages are context-free.
Rina Non-Context-Free Languages Consider L={a i b i c i |i 0}. 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 L={a i b i c i |i 0} 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 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 Recursive grammar Back to grammars. Grammar is recursive if it has: - direct recursion (A wAu) or - indirect recursion (A wBu,B zA)
Rina Direct recursion The following rule is called a direct recursion rule: A wAu where –w,u (VU )* and –A derives A in one step and –either w or u is not empty Why? – Unite rule.
Rina Indirect recursion A grammar having the following rules is called an indirect recursion grammar: A wBu B zA where –w,u,z (VU )* –either w or u is not empty –z is not empty –A derives A in two steps
Rina Recursive grammar Grammar is recursive if it has direct or indirect recursion. Example: S aS | b S aA A cS
Rina 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 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). n 1
Rina 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. n 1
Rina Infinite Language Example S uAz A vAy | 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 Example (cont.) For example w = uv 2 xy 2 z can be derived as follows: S uAz uvAyz uvvAyyz uvvxyyz The same pattern can be used to derive all words of the form w = uv k xy k z for all k 0.
Rina 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 = uv k xy k z for all k 0
Rina 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 w L 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 i 0, uv i xy i z L
Rina 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 n h.
Rina 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 n 2 (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 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 Proof We divide w into uvxyz accordingly to the parse tree. Each occurrence of R has a subtree under it.
Rina 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 Replacing the smaller by the larger repeatedly generates the string uv i xy i z at each i>0. Replacing the larger by the smaller generates the string uxz or uv i xy i z where i=0. That establishes condition 3 of lemma.
Rina 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 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 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 L={a i b i c i |i 0} We will use the pumping lemma to prove that L={a i b i c i |i 0} 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= a p b p c p. w L and |w|>p, so it can be pumped.
Rina L={a i b i c i |i 0} 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 uv 2 xy 2 z and it cannot contain the same number of a's, b's and c's. So w L.
Rina L={a i b i c i |i 0} 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 uv 2 xy 2 z and it cannot contain the same number of a's, b's and c's, because we pumped only two alphabet symbols. So w L.
Rina L={a i b i c i |i 0} 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={a i b i c i |i 0} is not CFL.
Rina Intersection CFL are not closed under intersection. Example: L1={a n b n c k |n,k>0} L2={a k b n c n | n,k>0} Both L1 and L2 are CFL L1 L2 = {a j b j c j | j>0} -not CFL
Rina 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 Complement If we take the compliment of this union we again obtain CFL. (L1' L2')'=L1 L2 - is CFL. But we know that {a n b n c k |n,k>0} {a k b n cn | n,k>0} = {a n b n c n | n>0} -not CFL
Rina Any Questions?