Pumping Lemma For Regular Language

Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 38

The Pumping Lemma for Regular

Languages

1
Some languages are not regular
When a language is regular?
if we are able to construct one of the following: DFA
or NFA or  -NFA or regular expression

When is it not?
If we can show that no FA can be built for a language

2
How to prove languages are not regular?

What if we cannot come up with any FA?


A)Can it be language that is not regular?
B) Or is it that we tried wrong approaches?

How do we decisively prove that a language is not


regular?

3
Pigeonhole Principle
• If we put n objects into m boxes(pigeonholes),
and if n>m, then a least one box must having
more than one item in it .
Pumping Lemma
• A pumping lemma is a method which is used
to prove that a language is not regular by
contradiction.
Pumping Lemma for Regular Languages
Let L be an infinite regular language.Then there exists
some positive integer N such that for every string
w  L s.t. |w|≥N, there exists and can be
decomposed w=xyz, such that:
1. y≠ 
2. |xy|≤N
3. For all k≥0, all strings of the form xykz  L

This property should hold for all regular languages.

Definition: N is called the “Pumping Lemma Constant”

6
Pumping Lemma: Proof
• L is regular => it should have a DFA.
– Set N := number of states in the DFA
• Any string wL, s.t. |w|≥N, should have the form:
w=a1a2…am, where m≥N
• Let the states traversed after reading the first N
symbols be: {p0,p1,… pN}
 ==> There are N+1 p-states, while there are only N DFA states
 ==> at least one state has to repeat
i.e, pi= pJwhere 0≤i<j≤N

7
Pumping Lemma: Proof…

 => We should be able to break w=xyz as follows:


 x=a1a2..ai; y=ai+1ai+2..aJ; z=aJ+1aJ+2..am
 x’s path will be p0..pi
 y’s path will be pi pi+1..pJ (but pi=pJ implying a loop)
 z’s path will be pJpJ+1..pm
 Now consider another yk (for k loops)
string wk=xykz , where k≥0
x z
 Case k=0 p0 pi pm
 DFA will reach the accept state pm pi=pj
 Case k>0
 DFA will loop for yk, and finally reach the accept state pm for z
 In either case, wk L
This proves part (3) of the lemma

8
Pumping Lemma: Proof…
• For part (1):
yk (for k loops)
– Since i<j, y ≠ 
x z
p0 pi pm
=pj
• For part (2):
– By PHP, the repetition of states has to occur
within the first N symbols in w
– ==> |xy|≤N

9
Example of using the Pumping Lemma to prove that
a language is not regular

Let L = {w | w is a binary string with equal number of 1s and


0s}
• Proof:
 By contradiction, let L be regular
 Let N be a constant (no. of states)
 Consider input w = 0N1N , |w|>N
 By pumping lemma, we should be able to break w=xyz,
such that:
1) y≠ 
2) |xy|≤N
3) For all k≥0, the string xykz is also in L

Template string w = 0N1N = 00 …. 011 … 1


N N 10
Proof…
 Because |xy|≤N, xy should contain only 0s
 (This and because y≠ , implies y=0+)
 Let k=0, then w=0N-k 0k 1N
 Therefore x can contain at most N-K 0s
 If k=0, then w=0N-k 1N (N-k ≠ N)
 Therefore, xy0z  L. Hence L is not regular

11
Exercise 2
Prove L = {0n10n | n≥ 1} is not regular

Note: This n is not to be confused with the pumping lemma


constant N. That can be different.

In other words, the above question is same as proving:


– L = {0m10m | m≥ 1} is not regular

12
Example 3: Pumping Lemma
Claim: L = { 0i | i is a perfect square} is not regular
• Proof:
 By contradiction, let L be regular.
 P/L should apply
 Let N = P/L constant
 Choose w=0N2
 By pumping lemma, w=xyz satisfying all three rules
 By rules (1) & (2), y has between 1 and N 0s
 By rule (3), any string of the form xykz is also in L for all k≥0
 Case k=0:
 #zeros (xy0z) = #zeros (xyz) - #zeros (y)
 N2 – N ≤ #zeros (xy0z) ≤ N2 - 1
 (N-1)2 < N2 - N ≤ #zeros (xy0z) ≤ N2 - 1 < N2
 xy0z  L
 But the above will complete the proof ONLY IF N>1.
 … (proof contd.. Next slide)

13
Example 3: Pumping Lemma
 (proof contd…)
 If the adversary pick N=1, then (N-1)2 ≤ N2 – N, and therefore the #zeros(xy0z) could end up
being a perfect square!
 This means that pumping down (i.e., setting k=0) is not giving us the proof!
 So lets try pumping up next…
 Case k=2:
 #zeros (xy2z) = #zeros (xyz) + #zeros (y)
 N2 + 1 ≤ #zeros (xy2z) ≤ N2 + N
 N2 < N2 + 1 ≤ #zeros (xy2z) ≤ N2 + N < (N+1)2
 xy2z  L

 (Notice that the above should hold for all possible N values of N>0. Therefore, this completes
the proof.)

14
Decision properties of regular
languages
Any “decision problem” looks like this:

Yes
Decision
Input problem
(generally solver
a question)
No

15
Membership question
• Decision Problem: Given L, is w in L?
• Possible answers: Yes or No
• Approach:
1. Build a DFA for L
2. Input w to the DFA
3. If the DFA ends in an accepting state, then yes;
otherwise no.

16
Emptiness test
• Decision Problem: Is L=Ø ?
• Approach:
On a DFA for L:
1. From the start state, run a reachability test, which
returns:
1. success: if there is at least one final state that is reachable from
the start state
2. failure: otherwise
2. L=Ø if and only if the reachability test fails

How to implement the reachability test?


17
Finiteness

• Decision Problem: Is L finite or infinite?


• Approach:
On a DFA for L:
1. Remove all states unreachable from the start state
2. Remove all states that cannot lead to any accepting state.
3. After removal, check for cycles in the resulting FA
4. L is finite if there are no cycles; otherwise it is infinite
• Another approach
– Build a regular expression and look for Kleene closure

How to implement steps 2 and 3?


18
Finiteness test - examples

Ex 1) Is the language of this DFA finite or infinite?

X FINITE
0

X q6 1

Ex 2) Is the language of this DFA finite or infinite?

INFINITE
X
0
to this
due
1
19
Equivalence & Minimization of
DFAs

20
Applications of interest
• Comparing two DFAs:
– L(DFA1) == L(DFA2)?

• How to minimize a DFA?


1. Remove unreachable states
2. Identify & condense equivalent states into one

21
When to call two states in a DFA
“equivalent”?
Two states p and q are said to be equivalent iff:
i) Any string w accepted by starting at p is also accepted by starting at
Past doesn’t matter - only future does!

q;

p
ii) Any string w rejected by starting at p is also
w rejected by starting at
AND q.
q

p
w
q
 p≡q
22
Computing equivalent states in a DFA
Table Filling Algorithm

A =
0 1
B = =
0 1 0
A C E G C x x =
1 0 1
0 1 D x x x =
B 1
D 1
F 0
H E x x x x =
1 0 F x x x x x =
0
Pass #0 G x x x = x x =
1. Mark accepting states ≠ non-accepting states
H x x = x x x x =
Pass #1
2. Compare every pair of states A B C D E F G H
3. Distinguish by one symbol transition
4. Mark = or ≠ or blank(tbd)
Pass #2
5. Compare every pair of states
6. Distinguish by up to two symbol transitions (until different or same or tbd)
…. 23
(keep repeating until table complete)
Table Filling Algorithm - step by step

A =
0 1
B =
0 1 0
A C E G C =
1 0 1
0 1 D =
B 1
D 1
F 0
H E =
1 0 F =
0
G =
H =
A B C D E F G H

24
Table Filling Algorithm - step by step

A =
0 1
B =
0 1 0
A C E G C =
1 0 1
0 1 D =
B 1
D 1
F 0
H E X X X X =
1 0 F X =
0
G X =
1. Mark X between accepting vs. non-accepting state
H X =
A B C D E F G H

25
Table Filling Algorithm - step by step

A =
0 1
B =
0 1 0
A C E G C X =
1 0 1
0 1 D X =
B 1
D 1
F 0
H E X X X X =
1 0 F X =
0
G X X =
1. Mark X between accepting vs. non-accepting state
H X X =
2. Look 1- hop away for distinguishing states or strings A B C D E F G H

26
Table Filling Algorithm - step by step

A =
0 1
B =
0 1 0
A C E G C X X =
1 0 1
0 1 D X X =
B 1
D 1
F 0
H E X X X X =
1 0 F X =
0
G X X X =
1. Mark X between accepting vs. non-accepting state
H X X X =
2. Look 1- hop away for distinguishing states or strings A B C D E F G H

27
Table Filling Algorithm - step by step

A =
0 1
B =
0 1 0
A C E G C X X =
1 0 1
0 1 D X X X =
B 1
D 1
F 0
H E X X X X =
1 0 F X X =
0
G X X X X =
1. Mark X between accepting vs. non-accepting state
H X X = X =
2. Look 1- hop away for distinguishing states or strings A B C D E F G H

28
Table Filling Algorithm - step by step

A =
0 1
B =
0 1 0
A C E G C X X =
1 0 1
0 1 D X X X =
B 1
D 1
F 0
H E X X X X =
1 0 F X X X =
0
G X X X = X =
1. Mark X between accepting vs. non-accepting state
H X X = X X =
2. Look 1- hop away for distinguishing states or strings A B C D E F G H

29
Table Filling Algorithm - step by step

A =
0 1
B =
0 1 0
A C E G C X X =
1 0 1
0 1 D X X X =
B 1
D 1
F 0
H E X X X X =
1 0 F X X X =
0
G X X X = X X =
1. Mark X between accepting vs. non-accepting state
H X X = X X X =
2. Look 1- hop away for distinguishing states or strings A B C D E F G H

30
Table Filling Algorithm - step by step

A =
0 1
B =
0 1 0
A C E G C X X =
1 0 1
0 1 D X X X =
B 1
D 1
F 0
H E X X X X =
1 0 F X X X =
0
G X X X = X X =
1. Mark X between accepting vs. non-accepting state
H X X = X X X X =
2. Look 1- hop away for distinguishing states or strings A B C D E F G H

31
Table Filling Algorithm - step by step

A =
0 1
B = =
0 1 0
A C E G C X X =
1 0 1
0 1 D X X X =
B 1
D 1
F 0
H E X X X X =
1 0 F X X X X X =
0
G X X X = X X =
1. Mark X between accepting vs. non-accepting state
H X X = X X X X =
2. Pass 1: A B C D E F G H
Look 1- hop away for distinguishing states or strings
3. Pass 2:
Look 1-hop away again for distinguishing states or strings
continue….

32
Table Filling Algorithm - step by step

A =
0 1
B = =
0 1 0
A C E G C X X =
1 0 1
0 1 D X X X =
B 1
D 1
F 0
H E X X X X =
1 0 F X X X X X =
0
G X X X = X X =
1. Mark X between accepting vs. non-accepting state
H X X = X X X X =
2. Pass 1: A B C D E F G H
Look 1- hop away for distinguishing states or strings
3. Pass 2:
Look 1-hop away again for distinguishing states or strings Equivalences:
continue…. • A=B
• C=H
• D=G 33
Table Filling Algorithm - step by
step

0 1 0 1

0 1 0 0 1
A C E G A C E
1 0 1 1 0
0 1 0
1 1 0 1
B D F H D F
1 0 1 0
0

Retrain only one copy for


each equivalence set of states

Equivalences:
• A=B
• C=H
• D=G 34
Table Filling Algorithm – special
case
A =
0 1
B =
0 1 0
A C E G C =
1 0 1
0 1 D =
1 1 0 E ? =
B D F H
1 F =
0
0 G =
H =
A B C D E F G H
Q) What happens if the input DFA
has more than one final state?
Can all final states initially be treated
as equivalent to one another?
35
Putting it all together …
How to minimize a DFA?
• Goal: Minimize the number of states in a DFA
• Algorithm:
1. Eliminate states unreachable fromfrom
Depth-first traversal thethestart state
start state
2. Identify and remove equivalent states
3. Output the resultant DFA
Table filling algorithm

36
Are Two DFAs Equivalent?

Unified DFA DFA1

q0 …
Is q0 ≡ q0’?
: if yes, then DFA1≡DFA2
DFA2
: else, not equiv.

q0’ …

1. Make a new dummy DFA by just putting together both DFAs


2. Run table-filling algorithm on the unified DFA
3. IF the start states of both DFAs are found to be equivalent,
THEN: DFA1≡ DFA2
ELSE: different 37
Summary
• How to prove languages are not regular?
– Pumping lemma & its applications

• Closure properties of regular languages

• Simplification of DFAs
– How to remove unreachable states?
– How to identify and collapse equivalent states?
– How to minimize a DFA?
– How to tell whether two DFAs are equivalent?

38

You might also like