Pumping Lemma Problem: –Let B be the language of all palindromes over {0,1} containing an equal...

64

Transcript of Pumping Lemma Problem: –Let B be the language of all palindromes over {0,1} containing an equal...

Page 1: Pumping Lemma Problem: –Let B be the language of all palindromes over {0,1} containing an equal number of 0s and 1s. Show that B is not CFL Solution:
Page 2: Pumping Lemma Problem: –Let B be the language of all palindromes over {0,1} containing an equal number of 0s and 1s. Show that B is not CFL Solution:

Pumping Lemma

Problem: – Let B be the language of all palindromes over {0,1} containing an

equal number of 0s and 1s. Show that B is not CFL

Solution:– We assume that B is a CFL and obtain a contradiction.

– Let p be the pumping constant of B that is guaranteed to exist by the pumping lemma. Select the string s = 0p1p1p0p .

– Clearly s is a member of B and of length at least p . We can show that no matter how we divide s into uvxyz, one of the conditions of the lemma is violated.

• Pumping lemma, | vxy |<= p , we can only place vxy in the following ways:

Page 3: Pumping Lemma Problem: –Let B be the language of all palindromes over {0,1} containing an equal number of 0s and 1s. Show that B is not CFL Solution:

1. vxy completely falls in the first 0p

– If we pump v and y , then the new string is no longer a palindrome, and the number of 0s will be greater than the number of 1s, which is a contradiction.

2. vxy falls between 0p and 1p

– In this case, v will only contain 0s while y will only contain 1s. So if we pump s , the new string is no longer palindrome, which is a contradiction.

3. vxy completely falls in the first 1p

– Similar to (1), after pumping s , the number of 1s will be greater than the number of 0s, which is a contradiction.

4. vxy falls between 1p and 1p

– After pumping s , the number of 1s will be greater than the number of 0s, which is a contradiction.

5. vxy completely falls in the second 1p

– This case is same with (3). 6. vxy falls between 1p and 0p

– Similar to (2), v will only contain 1s while y will only contain 0s. So if we pump s , the new string is no longer palindrome, which is a contradiction.

7. vxy completely falls in the second 0p– This case is same with (1).

Hence, B is not context free.

Page 4: Pumping Lemma Problem: –Let B be the language of all palindromes over {0,1} containing an equal number of 0s and 1s. Show that B is not CFL Solution:

• Let L = {w {0; 1}* : w = wR}.

• (a) Show that L is context-free by giving a context-free grammar for L.

• (b) Show that L is context-free by giving a pushdown automaton for L.

• (c) Show that L is not regular.

Page 5: Pumping Lemma Problem: –Let B be the language of all palindromes over {0,1} containing an equal number of 0s and 1s. Show that B is not CFL Solution:

CFG

• L is a symmetric language. Consider a context-free grammar for L : (V, Σ, R, S) , where

i. V = {S}

ii. Σ = {0,1}

iii. Rules:

S 0S0 |1S1| 0 |1|ε

iv. S = S V

Page 6: Pumping Lemma Problem: –Let B be the language of all palindromes over {0,1} containing an equal number of 0s and 1s. Show that B is not CFL Solution:

PDA

Page 7: Pumping Lemma Problem: –Let B be the language of all palindromes over {0,1} containing an equal number of 0s and 1s. Show that B is not CFL Solution:

Pumping Lemma

• Assume that L regular. Let p be the pumping constant given by the pumping lemma.

• Let s be the string 1p-1101p. • s can be split into three pieces, s = xyz , where for any i

>= 0 the string xyiz is in L .• According to the pumping lemma condition, we must

have | xy |<= p . If this is the case, then y must consist only of 1’s, so xyyz L . Therefore s cannot be pumped.

• This is a contradiction.

Page 8: Pumping Lemma Problem: –Let B be the language of all palindromes over {0,1} containing an equal number of 0s and 1s. Show that B is not CFL Solution:
Page 9: Pumping Lemma Problem: –Let B be the language of all palindromes over {0,1} containing an equal number of 0s and 1s. Show that B is not CFL Solution:

Pushdown Automaton -- PDAInput String

Stack

States

Page 10: Pumping Lemma Problem: –Let B be the language of all palindromes over {0,1} containing an equal number of 0s and 1s. Show that B is not CFL Solution:

Initial Stack Symbol

Stack

$

Stack

z

bottomspecial symbol

stackhead

top

Appears at time 0

Page 11: Pumping Lemma Problem: –Let B be the language of all palindromes over {0,1} containing an equal number of 0s and 1s. Show that B is not CFL Solution:

The States

q1 q2a, b c

Inputsymbol

Popsymbol

Pushsymbol

Page 12: Pumping Lemma Problem: –Let B be the language of all palindromes over {0,1} containing an equal number of 0s and 1s. Show that B is not CFL Solution:

q1 q2a, b c

a

b top

input

stack

a

Replaceeh

$eh

$

c

Page 13: Pumping Lemma Problem: –Let B be the language of all palindromes over {0,1} containing an equal number of 0s and 1s. Show that B is not CFL Solution:

q1 q2ca ,

a a

Pushb

eh

$eh

$

bc

top

input

stack

Page 14: Pumping Lemma Problem: –Let B be the language of all palindromes over {0,1} containing an equal number of 0s and 1s. Show that B is not CFL Solution:

q1 q2ba,

a a

Popb

eh

$eh

$

top

input

stack

Page 15: Pumping Lemma Problem: –Let B be the language of all palindromes over {0,1} containing an equal number of 0s and 1s. Show that B is not CFL Solution:

q1 q2 ,a

a a

No Changeb

eh

$eh

$

btop

input

stack

Page 16: Pumping Lemma Problem: –Let B be the language of all palindromes over {0,1} containing an equal number of 0s and 1s. Show that B is not CFL Solution:

q1 q2$,a

a a

Pop$ top

input

stack

Empty Stack

empty

The automaton HALTS

No possible transition after q2

Page 17: Pumping Lemma Problem: –Let B be the language of all palindromes over {0,1} containing an equal number of 0s and 1s. Show that B is not CFL Solution:

q1 q2ba $,

a a

Pop$ top

input

stack

A Possible Transition

b

Page 18: Pumping Lemma Problem: –Let B be the language of all palindromes over {0,1} containing an equal number of 0s and 1s. Show that B is not CFL Solution:

Non-Determinism

q1

q2a, b c

q3a, b c

q1 q2cb,

transition

PDAs are non-deterministic

Allowed non-deterministic transitions

Page 19: Pumping Lemma Problem: –Let B be the language of all palindromes over {0,1} containing an equal number of 0s and 1s. Show that B is not CFL Solution:

Example PDA

,

aa ,

ab,q0 q1 q2 q3

ab,

$$,

PDA M

}0:{)( nbaML nn

Page 20: Pumping Lemma Problem: –Let B be the language of all palindromes over {0,1} containing an equal number of 0s and 1s. Show that B is not CFL Solution:

}0:{)( nbaML nn

Basic Idea:

1. Push the a’s

on the stack2. Match the b’s on input

with a’s on stack

3. Match

found

,

aa ,

ab,q0 q1 q2 q3

ab,

$$,

Page 21: Pumping Lemma Problem: –Let B be the language of all palindromes over {0,1} containing an equal number of 0s and 1s. Show that B is not CFL Solution:

0q q1 q2 q3

Input

a a a b b b

Stack

$

aa , ab,

$$, , ab,

Page 22: Pumping Lemma Problem: –Let B be the language of all palindromes over {0,1} containing an equal number of 0s and 1s. Show that B is not CFL Solution:

q0 q1 q2 q3

a a a b b b$

$$, ,

aa ,

ab,

ab,

Input

Stack

Page 23: Pumping Lemma Problem: –Let B be the language of all palindromes over {0,1} containing an equal number of 0s and 1s. Show that B is not CFL Solution:

q0 q1 q2 q3

a a a b b b

$

a

$$, ,

aa ,

ab,

ab,

Input

Stack

Page 24: Pumping Lemma Problem: –Let B be the language of all palindromes over {0,1} containing an equal number of 0s and 1s. Show that B is not CFL Solution:

q0 q1 q2 q3

a a a b b b$

aa

$$, ,

aa ,

ab,

ab,

Input

Stack

Page 25: Pumping Lemma Problem: –Let B be the language of all palindromes over {0,1} containing an equal number of 0s and 1s. Show that B is not CFL Solution:

q0 q1 q2 q3

a a a b b b

$

aaa

$$, ,

aa ,

ab,

ab,

Input

Stack

Page 26: Pumping Lemma Problem: –Let B be the language of all palindromes over {0,1} containing an equal number of 0s and 1s. Show that B is not CFL Solution:

q0 q1 q2 q3

a a a b b b

$

aaa

$$, ,

aa ,

ab,

ab,

Input

Stack

Page 27: Pumping Lemma Problem: –Let B be the language of all palindromes over {0,1} containing an equal number of 0s and 1s. Show that B is not CFL Solution:

q0 q1 q2 q3

a a a b b b$

aa

$$, ,

aa ,

ab,

ab,

Input

Stack

Page 28: Pumping Lemma Problem: –Let B be the language of all palindromes over {0,1} containing an equal number of 0s and 1s. Show that B is not CFL Solution:

q0 q1 q2 q3

a a a b b b$

a

$$, ,

aa ,

ab,

ab,

Input

Stack

Page 29: Pumping Lemma Problem: –Let B be the language of all palindromes over {0,1} containing an equal number of 0s and 1s. Show that B is not CFL Solution:

q0 q1 q2 q3

a a a b b b

accept

$

$$, ,

aa ,

ab,

ab,

Input

Stack

Page 30: Pumping Lemma Problem: –Let B be the language of all palindromes over {0,1} containing an equal number of 0s and 1s. Show that B is not CFL Solution:

• A string is accepted if there is a computation such that:– All the input is consumed– The last state is an accepting state

– At the end of the computation,– we do not care about the stack contents– (the stack can be empty at the last state)

Page 31: Pumping Lemma Problem: –Let B be the language of all palindromes over {0,1} containing an equal number of 0s and 1s. Show that B is not CFL Solution:

}0:{ nbaL nn

is the language accepted by the PDA:

q0 q1 q2 q3 ,

aa ,

ab,

ab,

$$,

Page 32: Pumping Lemma Problem: –Let B be the language of all palindromes over {0,1} containing an equal number of 0s and 1s. Show that B is not CFL Solution:

aab

,

aa ,

ab,q0 q1 q2 q3

ab,

$$,

Page 33: Pumping Lemma Problem: –Let B be the language of all palindromes over {0,1} containing an equal number of 0s and 1s. Show that B is not CFL Solution:

0q q1 q2 q3

a a b$

,

aa ,

ab,

ab,

$$,

Page 34: Pumping Lemma Problem: –Let B be the language of all palindromes over {0,1} containing an equal number of 0s and 1s. Show that B is not CFL Solution:

0q q1 q2 q3

a a b$

,

aa ,

ab,

ab,

$$,

Page 35: Pumping Lemma Problem: –Let B be the language of all palindromes over {0,1} containing an equal number of 0s and 1s. Show that B is not CFL Solution:

0q q1 q2 q3

a a b$

a

,

aa ,

ab,

ab,

$$,

Page 36: Pumping Lemma Problem: –Let B be the language of all palindromes over {0,1} containing an equal number of 0s and 1s. Show that B is not CFL Solution:

0q q1 q2 q3

a a b$

aa

,

aa ,

ab,

ab,

$$,

Page 37: Pumping Lemma Problem: –Let B be the language of all palindromes over {0,1} containing an equal number of 0s and 1s. Show that B is not CFL Solution:

0q q1 q2 q3

a a b$

aa

,

aa ,

ab,

ab,

$$,

Page 38: Pumping Lemma Problem: –Let B be the language of all palindromes over {0,1} containing an equal number of 0s and 1s. Show that B is not CFL Solution:

0q q1 q2 q3

a a b$

aa

reject

,

aa ,

ab,

ab,

$$,

Page 39: Pumping Lemma Problem: –Let B be the language of all palindromes over {0,1} containing an equal number of 0s and 1s. Show that B is not CFL Solution:

Another PDA example

$$, q1 q2

bb

aa

,

,

,q0

bb

aa

,

,

}},{:{)( bavvvML R

Page 40: Pumping Lemma Problem: –Let B be the language of all palindromes over {0,1} containing an equal number of 0s and 1s. Show that B is not CFL Solution:

}},{:{)( bavvvML R

1. Push v

on stack2. Guess

middle

of input

3. Match vR on input

with v on stack

4. Match

found

$$, q1 q2

,q0

bb

aa

,

,

bb

aa

,

,

Page 41: Pumping Lemma Problem: –Let B be the language of all palindromes over {0,1} containing an equal number of 0s and 1s. Show that B is not CFL Solution:

$

q1 q2q0

a ab b

bb

aa

,

,

,

bb

aa

,

,

$$,

Page 42: Pumping Lemma Problem: –Let B be the language of all palindromes over {0,1} containing an equal number of 0s and 1s. Show that B is not CFL Solution:

a ab

$

q1 q2q0

ab

bb

aa

,

,

,

bb

aa

,

,

$$,

Page 43: Pumping Lemma Problem: –Let B be the language of all palindromes over {0,1} containing an equal number of 0s and 1s. Show that B is not CFL Solution:

$

q1 q2q0

aa ab b

b

bb

aa

,

,

,

bb

aa

,

,

$$,

Page 44: Pumping Lemma Problem: –Let B be the language of all palindromes over {0,1} containing an equal number of 0s and 1s. Show that B is not CFL Solution:

$

q1 q2q0

aa ab b

b

Guess the middle

of string

bb

aa

,

,

,

bb

aa

,

,

$$,

Page 45: Pumping Lemma Problem: –Let B be the language of all palindromes over {0,1} containing an equal number of 0s and 1s. Show that B is not CFL Solution:

$

q1 q2q0

aa ab b

b

bb

aa

,

,

,

bb

aa

,

,

$$,

Page 46: Pumping Lemma Problem: –Let B be the language of all palindromes over {0,1} containing an equal number of 0s and 1s. Show that B is not CFL Solution:

$

1q q2q0

a ab b a

bb

aa

,

,

,

bb

aa

,

,

$$,

Page 47: Pumping Lemma Problem: –Let B be the language of all palindromes over {0,1} containing an equal number of 0s and 1s. Show that B is not CFL Solution:

$

q1q0

a ab b

accept

q2

bb

aa

,

,

,

bb

aa

,

,

$$,

Page 48: Pumping Lemma Problem: –Let B be the language of all palindromes over {0,1} containing an equal number of 0s and 1s. Show that B is not CFL Solution:

Rejection Example:

Input

$

q1 q2q0

a b b b

bb

aa

,

,

,

bb

aa

,

,

$$,

Page 49: Pumping Lemma Problem: –Let B be the language of all palindromes over {0,1} containing an equal number of 0s and 1s. Show that B is not CFL Solution:

$

q1 q2q0

aa b b b

bb

aa

,

,

,

bb

aa

,

,

$$,

Page 50: Pumping Lemma Problem: –Let B be the language of all palindromes over {0,1} containing an equal number of 0s and 1s. Show that B is not CFL Solution:

$

q1 q2q0

ab

a b b b

bb

aa

,

,

,

bb

aa

,

,

$$,

Page 51: Pumping Lemma Problem: –Let B be the language of all palindromes over {0,1} containing an equal number of 0s and 1s. Show that B is not CFL Solution:

$

q1 q2q0

ab

Guess the middle

of string

a b b b

bb

aa

,

,

,

bb

aa

,

,

$$,

Page 52: Pumping Lemma Problem: –Let B be the language of all palindromes over {0,1} containing an equal number of 0s and 1s. Show that B is not CFL Solution:

$

q1 q2q0

ab

a b b b

bb

aa

,

,

,

bb

aa

,

,

$$,

Page 53: Pumping Lemma Problem: –Let B be the language of all palindromes over {0,1} containing an equal number of 0s and 1s. Show that B is not CFL Solution:

$

1q q2q0

aa b b b

There is no possible transition.

Input is not consumed

bb

aa

,

,

,

bb

aa

,

,

$$,

Page 54: Pumping Lemma Problem: –Let B be the language of all palindromes over {0,1} containing an equal number of 0s and 1s. Show that B is not CFL Solution:

Another computation on same string:

$

q1 q2q0

a b b b

bb

aa

,

,

,

bb

aa

,

,

$$,

Page 55: Pumping Lemma Problem: –Let B be the language of all palindromes over {0,1} containing an equal number of 0s and 1s. Show that B is not CFL Solution:

$

q1 q2q0

aa b b b

bb

aa

,

,

,

bb

aa

,

,

$$,

Page 56: Pumping Lemma Problem: –Let B be the language of all palindromes over {0,1} containing an equal number of 0s and 1s. Show that B is not CFL Solution:

$

q1 q2q0

ab

a b b b

bb

aa

,

,

,

bb

aa

,

,

$$,

Page 57: Pumping Lemma Problem: –Let B be the language of all palindromes over {0,1} containing an equal number of 0s and 1s. Show that B is not CFL Solution:

$ab

a b b b

q1 q2q0

b

bb

aa

,

,

,

bb

aa

,

,

$$,

Page 58: Pumping Lemma Problem: –Let B be the language of all palindromes over {0,1} containing an equal number of 0s and 1s. Show that B is not CFL Solution:

a b b b

q1 q2q0

$abbb

bb

aa

,

,

,

bb

aa

,

,

$$,

Page 59: Pumping Lemma Problem: –Let B be the language of all palindromes over {0,1} containing an equal number of 0s and 1s. Show that B is not CFL Solution:

a b b b

$abbb

q1 q2q0

final state

is not reached

bb

aa

,

,

,

bb

aa

,

,

$$,

Page 60: Pumping Lemma Problem: –Let B be the language of all palindromes over {0,1} containing an equal number of 0s and 1s. Show that B is not CFL Solution:

q1 q2q0

)(MLabbb

bb

aa

,

,

,

bb

aa

,

,

$$,

Page 61: Pumping Lemma Problem: –Let B be the language of all palindromes over {0,1} containing an equal number of 0s and 1s. Show that B is not CFL Solution:

Another PDA example

ab

aa

,

,

q0

M

)}()( ,prefix every in

:},{{)( *

vnvnv

bawML

ba

aababbb (r)

Page 62: Pumping Lemma Problem: –Let B be the language of all palindromes over {0,1} containing an equal number of 0s and 1s. Show that B is not CFL Solution:

Pushing Strings

q1 q2a, b w

Input

symbol

Pop

symbolPush

string

Page 63: Pumping Lemma Problem: –Let B be the language of all palindromes over {0,1} containing an equal number of 0s and 1s. Show that B is not CFL Solution:

q1 q2a, b cdf

a

b top

a

Push

eh h

e

cdf

pushed

string

Example:

$ $

Page 64: Pumping Lemma Problem: –Let B be the language of all palindromes over {0,1} containing an equal number of 0s and 1s. Show that B is not CFL Solution:

Another PDA example

$$, q1 q2

a, $ 0$a, 0 00

1,a

b, $ 1$b, 1 11

0,b

)}()(:},{{)( * wnwnbawML ba

abbbaa