CS 301 Lecture 06 Nonregular languages and the pumping lemma - - PowerPoint PPT Presentation

cs 301
SMART_READER_LITE
LIVE PREVIEW

CS 301 Lecture 06 Nonregular languages and the pumping lemma - - PowerPoint PPT Presentation

CS 301 Lecture 06 Nonregular languages and the pumping lemma Stephen Checkoway February 5, 2018 1 / 23 DFA M 1 b a a q 0 q 1 q 2 b a,b q 3 a,b 2 / 23 DFA M 1 Strings in the language b aa a a q 0 q 1 q 2 aba abba


slide-1
SLIDE 1

CS 301

Lecture 06 – Nonregular languages and the pumping lemma Stephen Checkoway February 5, 2018

1 / 23

slide-2
SLIDE 2

DFA M1

q0 q1 q2 q3 a b a b a,b a,b

2 / 23

slide-3
SLIDE 3

DFA M1

q0 q1 q2 q3 a b a b a,b a,b Strings in the language

  • aa
  • aba
  • abba
  • abbba
  • abka for all k ≥ 0

2 / 23

slide-4
SLIDE 4

DFA M1

q0 q1 q2 q3 a b a b a,b a,b Strings in the language

  • aa
  • aba
  • abba
  • abbba
  • abka for all k ≥ 0

All of the strings w ∈ L(M1) s.t. ∣w∣ ≥ 3 have a curious property: w can be written as w = xyz where

1 ∣y∣ > 0 and 2 xyiz ∈ L(M1) for all i ≥ 0

2 / 23

slide-5
SLIDE 5

DFA M2

q0 q1 q2 q3 q4 a b a b a,b a b a,b

3 / 23

slide-6
SLIDE 6

DFA M2

q0 q1 q2 q3 q4 a b a b a,b a b a,b Strings in the language include

  • a
  • b
  • ba
  • aba
  • abb
  • baba
  • abbba
  • bababb

Again, strings w ∈ L(M2) s.t. ∣w∣ ≥ 3 can be written as w = xyz with ∣y∣ > 0 and xyiz ∈ L(M2). E.g., x = ba, y = ba, z = ε

  • xy0z = ba
  • xy1z = baba
  • xy2z = bababa
  • . . .

3 / 23

slide-7
SLIDE 7

DFA M3

q0 q1 q2 a b a b a,b

4 / 23

slide-8
SLIDE 8

DFA M3

q0 q1 q2 a b a b a,b L(M3) = {ambn ∣ m, n ≥ 0} Strings w ∈ L(M3) s.t. ∣w∣ ≥ 1 have the same property. E.g., x = ε, y = a, z = abb

  • xy0z = abb
  • xy1z = aabb
  • xy2z = aaabb
  • xyiz = ai+1bb

4 / 23

slide-9
SLIDE 9

DFA M3

q0 q1 q2 a b a b a,b L(M3) = {ambn ∣ m, n ≥ 0} Strings w ∈ L(M3) s.t. ∣w∣ ≥ 1 have the same property. E.g., x = ε, y = a, z = abb

  • xy0z = abb
  • xy1z = aabb
  • xy2z = aaabb
  • xyiz = ai+1bb

Not every way we split the strings works x = a, y = ab, z = b

  • xy0z = ab ∈ L(M3)
  • xy1z = aabb ∈ L(M3)
  • xy2z = aababb ∉ L(M3)

4 / 23

slide-10
SLIDE 10

DFA M4

q0 q1 q2 q3 a b a,b a,b a,b

5 / 23

slide-11
SLIDE 11

DFA M4

q0 q1 q2 q3 a b a,b a,b a,b L(M4) = {ε, b, ba, bb} L(M4) doesn’t appear to have this property (unless we say it holds for all strings in L(M4) with length at least 3 because there are no such strings)

5 / 23

slide-12
SLIDE 12

What do M1, M2, and M3 have that M4 lacks?

q0 q1 q2 q3 a b a b a,b a,b q0 q1 q2 q3 q4 a b a b a,b a b a,b q0 q1 q2 a b a b a,b q0 q1 q2 q3 a b a,b a,b a,b M1: M2: M3: M4:

6 / 23

slide-13
SLIDE 13

Repeated state for some string in the language

M1, M2, and M3 all have a repeated state in some accepting computation q0 q1 q2 q3 a b a b a,b a,b On input aba, M1 goes through states q0, q1, q1, q2 State q1 is repeated so we can repeat it 0 or more times by following the loop on b

7 / 23

slide-14
SLIDE 14

M2

q0 q1 q2 q3 q4 a b a b a,b a b a,b On input baba, M2 goes through states q0, q3, q1, q2, q1 State q1 is repeated so we can perform the q1 → q2 → q1 sequence corresponding to input ba 0 or more times

8 / 23

slide-15
SLIDE 15

M3

q0 q1 q2 a b a b a,b On input aabb, M2 goes through states q0, q0, q0, q1, q1 State q0 is repeated so we can perform the q0 → q0 sequence corresponding to input a 0 or more times

9 / 23

slide-16
SLIDE 16

M4

q0 q1 q2 q3 a b a,b a,b a,b None of the strings in L(M4) lead to a repeated state As mentioned, we can “cheat” and say that the property holds for strings of length at least 3 since L(M4) has no strings of length at least 3

10 / 23

slide-17
SLIDE 17

Pumpable languages

A language A is said to be pumpable if there exists an integer p > 0 s.t. for all strings w ∈ A with ∣w∣ ≥ p, there exist strings x, y, z ∈ Σ∗ with w = xyz s.t.

1 xyiz ∈ A for all i ≥ 0 2 ∣y∣ > 0 3 ∣xy∣ ≤ p

The integer p is called the pumping length

11 / 23

slide-18
SLIDE 18

Pumpable languages

A language A is said to be pumpable if there exists an integer p > 0 s.t. for all strings w ∈ A with ∣w∣ ≥ p, there exist strings x, y, z ∈ Σ∗ with w = xyz s.t.

1 xyiz ∈ A for all i ≥ 0 2 ∣y∣ > 0 3 ∣xy∣ ≤ p

The integer p is called the pumping length Almost certainly the most complicated mathematical definition you’ve seen: ∃p > 0. ∀w ∈ A. ∃x, y, z ∈ Σ∗. ∀i ≥ 0. [. . . ] Contrast with the definition of a continuous function f ∶ R → R from calculus ∀ε > 0. ∃δ > 0. [. . . ]

11 / 23

slide-19
SLIDE 19

A two-player game

Player One (∃) Player Two (∀) Claims A is pumpable with p.l. p A, p − − − − − − − − − − − − − − − → Picks w ∈ A s.t. ∣w∣ ≥ p w ← − − − − − − − − − − − − − − − Picks x, y, z s.t. w = xyz x, y, z − − − − − − − − − − − − − − − → Checks 3 conditions Player One “wins” the game if

1 xyiz ∈ A for all i ≥ 0 2 ∣y∣ > 0 3 ∣xy∣ ≤ p

Player One can win if and only if A is pumpable

12 / 23

slide-20
SLIDE 20

Pumping lemma for regular languages

Theorem (Pumping lemma)

Regular languages are pumpable. Note: The converse is not true! There are pumpable languages that are not regular

13 / 23

slide-21
SLIDE 21

Proof

Let M = (Q, Σ, δ, q0, F) be a DFA with L(M) = A and set p = ∣Q∣. If A contains no strings of length at least p, then we’re finished since A is pumpable with pumping length p.

14 / 23

slide-22
SLIDE 22

Proof

Let M = (Q, Σ, δ, q0, F) be a DFA with L(M) = A and set p = ∣Q∣. If A contains no strings of length at least p, then we’re finished since A is pumpable with pumping length p. Otherwise, let w be a string in A of length n ≥ p. Write w = w1w2⋯wn where each wi ∈ Σ. Let r0, r1, . . . , rn be the accepting computation of M on w. By the pigeonhole principle, in the first p + 1 states (r0, r1, . . . , rp), there are states rj = rk s.t. 0 ≤ j < k ≤ p.

14 / 23

slide-23
SLIDE 23

Proof

Set x = w1w2⋯wj y = wj+1wj+2⋯wk z = wk+1wk+2⋯wn. input:

x

  • w1 w2 ⋯ wj

y

  • wj+1 wj+2 ⋯ wk

z

  • wk+1 wk+2 ⋯ wn

states: r0 r1 r2 ⋯ rj rj+1 rj+2 ⋯ rk rk+1 rk+2 ⋯ rn Remember δ(rm−1, wm) = rm for all 1 ≤ m ≤ n

2 ∣y∣ = k − j > 0 3 ∣xy∣ ≤ p because k ≤ p

15 / 23

slide-24
SLIDE 24

Proof

1 xyiz ?

∈ A δ(rm−1, wm) = rm ∀m

16 / 23

slide-25
SLIDE 25

Proof

1 xyiz ?

∈ A δ(rm−1, wm) = rm ∀m i = 0

x

  • w1 w2 ⋯ wj

z

  • wk+1 wk+2 ⋯ wn

r0 r1 r2 ⋯ rj

16 / 23

slide-26
SLIDE 26

Proof

1 xyiz ?

∈ A δ(rm−1, wm) = rm ∀m δ(rj, wk+1) = δ(rk, wk+1) = rk+1 i = 0

x

  • w1 w2 ⋯ wj

z

  • wk+1 wk+2 ⋯ wn

r0 r1 r2 ⋯ rj

16 / 23

slide-27
SLIDE 27

Proof

1 xyiz ?

∈ A δ(rm−1, wm) = rm ∀m δ(rj, wk+1) = δ(rk, wk+1) = rk+1 i = 0

x

  • w1 w2 ⋯ wj

z

  • wk+1 wk+2 ⋯ wn

r0 r1 r2 ⋯ rj rk+1 rk+2 ⋯ rn

16 / 23

slide-28
SLIDE 28

Proof

1 xyiz ?

∈ A δ(rm−1, wm) = rm ∀m δ(rj, wk+1) = δ(rk, wk+1) = rk+1 i = 0

x

  • w1 w2 ⋯ wj

z

  • wk+1 wk+2 ⋯ wn

r0 r1 r2 ⋯ rj rk+1 rk+2 ⋯ rn i = 1

x

  • w1 w2 ⋯ wj

y

  • wj+1 wj+2 ⋯ wk

z

  • wk+1 wk+2 ⋯ wn

r0 r1 r2 ⋯ rj rj+1 rj+2 ⋯ rk rk+1 rk+2 ⋯ rn

16 / 23

slide-29
SLIDE 29

Proof

1 xyiz ?

∈ A δ(rm−1, wm) = rm ∀m δ(rj, wk+1) = δ(rk, wk+1) = rk+1 i = 0

x

  • w1 w2 ⋯ wj

z

  • wk+1 wk+2 ⋯ wn

r0 r1 r2 ⋯ rj rk+1 rk+2 ⋯ rn i = 1

x

  • w1 w2 ⋯ wj

y

  • wj+1 wj+2 ⋯ wk

z

  • wk+1 wk+2 ⋯ wn

r0 r1 r2 ⋯ rj rj+1 rj+2 ⋯ rk rk+1 rk+2 ⋯ rn i = 2

x

  • w1 w2 ⋯ wj

y

  • wj+1 wj+2 ⋯ wk

y

  • wj+1 wj+2 ⋯ wk

z

  • wk+1 wk+2 ⋯ wn

r0 r1 r2 ⋯ rj rj+1 rj+2 ⋯ rk

16 / 23

slide-30
SLIDE 30

Proof

1 xyiz ?

∈ A δ(rm−1, wm) = rm ∀m δ(rj, wk+1) = δ(rk, wk+1) = rk+1 δ(rk, wj+1) = δ(rj, wj+1) = rj+1 i = 0

x

  • w1 w2 ⋯ wj

z

  • wk+1 wk+2 ⋯ wn

r0 r1 r2 ⋯ rj rk+1 rk+2 ⋯ rn i = 1

x

  • w1 w2 ⋯ wj

y

  • wj+1 wj+2 ⋯ wk

z

  • wk+1 wk+2 ⋯ wn

r0 r1 r2 ⋯ rj rj+1 rj+2 ⋯ rk rk+1 rk+2 ⋯ rn i = 2

x

  • w1 w2 ⋯ wj

y

  • wj+1 wj+2 ⋯ wk

y

  • wj+1 wj+2 ⋯ wk

z

  • wk+1 wk+2 ⋯ wn

r0 r1 r2 ⋯ rj rj+1 rj+2 ⋯ rk

16 / 23

slide-31
SLIDE 31

Proof

1 xyiz ?

∈ A δ(rm−1, wm) = rm ∀m δ(rj, wk+1) = δ(rk, wk+1) = rk+1 δ(rk, wj+1) = δ(rj, wj+1) = rj+1 i = 0

x

  • w1 w2 ⋯ wj

z

  • wk+1 wk+2 ⋯ wn

r0 r1 r2 ⋯ rj rk+1 rk+2 ⋯ rn i = 1

x

  • w1 w2 ⋯ wj

y

  • wj+1 wj+2 ⋯ wk

z

  • wk+1 wk+2 ⋯ wn

r0 r1 r2 ⋯ rj rj+1 rj+2 ⋯ rk rk+1 rk+2 ⋯ rn i = 2

x

  • w1 w2 ⋯ wj

y

  • wj+1 wj+2 ⋯ wk

y

  • wj+1 wj+2 ⋯ wk

z

  • wk+1 wk+2 ⋯ wn

r0 r1 r2 ⋯ rj rj+1 rj+2 ⋯ rk rj+1 rj+2 ⋯ rk

16 / 23

slide-32
SLIDE 32

Proof

1 xyiz ?

∈ A δ(rm−1, wm) = rm ∀m δ(rj, wk+1) = δ(rk, wk+1) = rk+1 δ(rk, wj+1) = δ(rj, wj+1) = rj+1 i = 0

x

  • w1 w2 ⋯ wj

z

  • wk+1 wk+2 ⋯ wn

r0 r1 r2 ⋯ rj rk+1 rk+2 ⋯ rn i = 1

x

  • w1 w2 ⋯ wj

y

  • wj+1 wj+2 ⋯ wk

z

  • wk+1 wk+2 ⋯ wn

r0 r1 r2 ⋯ rj rj+1 rj+2 ⋯ rk rk+1 rk+2 ⋯ rn i = 2

x

  • w1 w2 ⋯ wj

y

  • wj+1 wj+2 ⋯ wk

y

  • wj+1 wj+2 ⋯ wk

z

  • wk+1 wk+2 ⋯ wn

r0 r1 r2 ⋯ rj rj+1 rj+2 ⋯ rk rj+1 rj+2 ⋯ rk rk+1 rk+2 ⋯ rn

16 / 23

slide-33
SLIDE 33

Proof

Starting in state rj, when M reads y, it ends in state rk = rj. Therefore, when M runs on xyiz, it

1 starts in state r0 = q0 and after reading x is in state rj; 2 for each of the i copies of y, it is in state rj, reads y, and moves to state rk = rj;

and

3 from state rk, it reads z and ends in state rn ∈ F

Therefore M accepts xyiz so

1 xyiz ∈ A 2 ∣y∣ > 0 3 ∣xy∣ ≤ p.

Therefore, A is pumpable.

17 / 23

slide-34
SLIDE 34

Proving languages are not regular

If we want to prove language A is not regular,

1 Assume A is regular

18 / 23

slide-35
SLIDE 35

Proving languages are not regular

If we want to prove language A is not regular,

1 Assume A is regular 2 Since A is regular, it is pumpable with pumping length p

18 / 23

slide-36
SLIDE 36

Proving languages are not regular

If we want to prove language A is not regular,

1 Assume A is regular 2 Since A is regular, it is pumpable with pumping length p 3 Construct a string w ∈ A of length at least p

18 / 23

slide-37
SLIDE 37

Proving languages are not regular

If we want to prove language A is not regular,

1 Assume A is regular 2 Since A is regular, it is pumpable with pumping length p 3 Construct a string w ∈ A of length at least p 4 Show that every partition of w into xyz such that ∣xy∣ ≤ p and ∣y∣ > 0 yields

some i such that xyiz ∉ A

18 / 23

slide-38
SLIDE 38

Proving languages are not regular

If we want to prove language A is not regular,

1 Assume A is regular 2 Since A is regular, it is pumpable with pumping length p 3 Construct a string w ∈ A of length at least p 4 Show that every partition of w into xyz such that ∣xy∣ ≤ p and ∣y∣ > 0 yields

some i such that xyiz ∉ A

5 This contradicts the pumping lemma so our assumption must be false, namely A

is not regular

18 / 23

slide-39
SLIDE 39

Example

Let’s prove A = {0n1n ∣ n ≥ 0} is not regular

19 / 23

slide-40
SLIDE 40

Example

Let’s prove A = {0n1n ∣ n ≥ 0} is not regular Assume A is regular with pumping length p Now we need to pick a string w ∈ A with length ∣w∣ ≥ p

19 / 23

slide-41
SLIDE 41

Example

Let’s prove A = {0n1n ∣ n ≥ 0} is not regular Assume A is regular with pumping length p Now we need to pick a string w ∈ A with length ∣w∣ ≥ p Let w = 0p1p which has length 2p ≥ p Consider xyz = w such that ∣xy∣ ≤ p and ∣y∣ > 0 We got to choose w, but we don’t get to choose x, y, and z We have to consider all possible choices! What are the possible values of x, y, and z?

19 / 23

slide-42
SLIDE 42

Example

Let’s prove A = {0n1n ∣ n ≥ 0} is not regular Assume A is regular with pumping length p Now we need to pick a string w ∈ A with length ∣w∣ ≥ p Let w = 0p1p which has length 2p ≥ p Consider xyz = w such that ∣xy∣ ≤ p and ∣y∣ > 0 We got to choose w, but we don’t get to choose x, y, and z We have to consider all possible choices! What are the possible values of x, y, and z? x and y consist solely of 0s and z has the rest of the p 0s followed by p 1s: x = 0m, y = 0n, z = 0p−m−n1p where n > 0

19 / 23

slide-43
SLIDE 43

Example

x = 0m, y = 0n, z = 0p−m−n1p where n > 0 Now we need to find an i ≥ 0 such that xyiz ∉ A What value of i should we choose?

20 / 23

slide-44
SLIDE 44

Example

x = 0m, y = 0n, z = 0p−m−n1p where n > 0 Now we need to find an i ≥ 0 such that xyiz ∉ A What value of i should we choose? In this case any i ≠ 1 works, so let’s go with i = 0 (“pumping down”) xy0z = xz = 0p−n1p Since n > 0, p − n ≠ p so xy0z ∉ A and thus A is not regular

20 / 23

slide-45
SLIDE 45

Palindromes are not regular

Prove B = {w ∣ w ∈ {0, 1}∗ and w = wR} is not regular

21 / 23

slide-46
SLIDE 46

Palindromes are not regular

Prove B = {w ∣ w ∈ {0, 1}∗ and w = wR} is not regular Assume B is regular with pumping length p We need to pick w; what should we pick?

21 / 23

slide-47
SLIDE 47

Palindromes are not regular

Prove B = {w ∣ w ∈ {0, 1}∗ and w = wR} is not regular Assume B is regular with pumping length p We need to pick w; what should we pick? Let w = 0p10p Thus, x = 0m, y = 0n, and z = 0p−m−n10p

21 / 23

slide-48
SLIDE 48

Palindromes are not regular

Prove B = {w ∣ w ∈ {0, 1}∗ and w = wR} is not regular Assume B is regular with pumping length p We need to pick w; what should we pick? Let w = 0p10p Thus, x = 0m, y = 0n, and z = 0p−m−n10p Let’s “pump up” this time and try i = 2 xy2z = 0p+n10p ∉ B Therefore, B is not regular

21 / 23

slide-49
SLIDE 49

Subsets of regular languages

True or false. If A is a regular language and B ⊆ A, then B is regular.

22 / 23

slide-50
SLIDE 50

Subsets of regular languages

True or false. If A is a regular language and B ⊆ A, then B is regular. FALSE! Every language over Σ is a subset of Σ∗ which is regular

22 / 23

slide-51
SLIDE 51

Subsets of regular languages

True or false. If A is a regular language and B ⊆ A, then B is regular. FALSE! Every language over Σ is a subset of Σ∗ which is regular What’s wrong with this argument? Since A is regular, there is a DFA M that recognizes A Since B ⊆ A, M accepts every string in B so B is regular

22 / 23

slide-52
SLIDE 52

Subsets of regular languages

True or false. If A is a regular language and B ⊆ A, then B is regular. FALSE! Every language over Σ is a subset of Σ∗ which is regular What’s wrong with this argument? Since A is regular, there is a DFA M that recognizes A Since B ⊆ A, M accepts every string in B so B is regular It’s missing the fact that for B to be regular there needs to be a DFA M′ that accepts every string in B and rejects every string not in B Σ accepts every string in Σ∗

22 / 23

slide-53
SLIDE 53

More nonregular languages

  • C = {0m1n0m ∣ m, n ≥ 0}
  • D = {0m1n ∣ m ≤ n}
  • E = {w ∣ w ∈ {0, 1}∗ and w has the same number of 0s and 1s}

23 / 23