Presentation is loading. Please wait.

Presentation is loading. Please wait.

Complexity and Computability Theory I Lecture #12 Instructor: Rina Zviel-Girshin Lea Epstein.

Similar presentations


Presentation on theme: "Complexity and Computability Theory I Lecture #12 Instructor: Rina Zviel-Girshin Lea Epstein."— Presentation transcript:

1 Complexity and Computability Theory I Lecture #12 Instructor: Rina Zviel-Girshin Lea Epstein

2 Rina Zviel-Girshin @ASC2 Overview Push-down automata - examples The pumping lemma for CFL Examples Interesting facts

3 Rina Zviel-Girshin @ASC3 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

4 Rina Zviel-Girshin @ASC4 L = {a i b j | j>i  0}

5 Rina Zviel-Girshin @ASC5 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

6 Rina Zviel-Girshin @ASC6 Example

7 Rina Zviel-Girshin @ASC7 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

8 Rina Zviel-Girshin @ASC8 L = {a i b i c j d j |i,j  0}

9 Rina Zviel-Girshin @ASC9 Context-Free Languages Languages decided by PDA called –context-free languages. Not all languages are context-free.

10 Rina Zviel-Girshin @ASC10 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.

11 Rina Zviel-Girshin @ASC11 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.

12 Rina Zviel-Girshin @ASC12 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.

13 Rina Zviel-Girshin @ASC13 Recursive grammar Back to grammars. Grammar is recursive if it has: - direct recursion (A  wAu) or - indirect recursion (A  wBu,B  zA)

14 Rina Zviel-Girshin @ASC14 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.

15 Rina Zviel-Girshin @ASC15 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

16 Rina Zviel-Girshin @ASC16 Recursive grammar Grammar is recursive if it has direct or indirect recursion. Example: S  aS | b S  aA A  cS

17 Rina Zviel-Girshin @ASC17 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)

18 Rina Zviel-Girshin @ASC18 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

19 Rina Zviel-Girshin @ASC19 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

20 Rina Zviel-Girshin @ASC20 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).

21 Rina Zviel-Girshin @ASC21 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.

22 Rina Zviel-Girshin @ASC22 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

23 Rina Zviel-Girshin @ASC23 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

24 Rina Zviel-Girshin @ASC24 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.

25 Rina Zviel-Girshin @ASC25 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.

26 Rina Zviel-Girshin @ASC26 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.

27 Rina Zviel-Girshin @ASC27 Proof We divide w into uvxyz accordingly to the parse tree. Each occurrence of R has a subtree under it.

28 Rina Zviel-Girshin @ASC28 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.

29 Rina Zviel-Girshin @ASC29 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.

30 Rina Zviel-Girshin @ASC30 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.

31 Rina Zviel-Girshin @ASC31 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

32 Rina Zviel-Girshin @ASC32 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.

33 Rina Zviel-Girshin @ASC33 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.

34 Rina Zviel-Girshin @ASC34 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.

35 Rina Zviel-Girshin @ASC35 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.

36 Rina Zviel-Girshin @ASC36 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.

37 Rina Zviel-Girshin @ASC37 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

38 Rina Zviel-Girshin @ASC38 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).

39 Rina Zviel-Girshin @ASC39 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

40 Rina Zviel-Girshin @ASC40 Any Questions?


Download ppt "Complexity and Computability Theory I Lecture #12 Instructor: Rina Zviel-Girshin Lea Epstein."

Similar presentations


Ads by Google