MENOUFIAUNIVERSITY
FACULTYOFCOMPUTERSANDINFORMATION
جامعة المنوفية
Third Level (Second Semester)
Software Engineering, CS Dept., (CS314)
FORMAL LANGUAGES AND
AUTOMATA THEORY
Dr. Hamdy M. Mousa
Today Proverb
العلم نور
Knowledge is power
Properties of
Regular Languages
Topics
In this lecture:
1) How to prove whether a given language
is regular or not?
2) Closure properties of regular languages
3) Minimization of DFAs
Some languages are not regular
Language is Regular:
if we are able to construct one of
the following:
• DFA or NFA or -NFA or regular
expression
When is Language not Regular?
If we can show that no FA can be
built for a language
Properties
• Closure property:
• These properties let us build recognizers for
languages that are constructed from other
languages by certain operations.
• Decision Properties:
• Algorithms for answering important questions
about automata
• A central example is an algorithm for deciding
whether two automata define the same
language.
Prove Regular Languages
• 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?
Non-regular Language
Example of a non-regular language:
Let L = {w | w is of the form 0n1n , for all n≥0}
• Hypothesis: L is not regular
• Intuitive rationale: How do you keep track
of a running count in an FA?
• A more formal rationale:
➢ By contradiction, if L is regular then there should exist a
DFA for L.
➢ Let k = number of states in that DFA.
➢ Consider the special word w= 0k1k => w L
➢ DFA is in some state pi, after consuming the first i
symbols in w
Rationale…
➢ Let {p0,p1,… pk} be the sequence of
states that the DFA should have visited
after consuming the first k symbols in w
which is 0k
➢ But there are only k states in the DFA!
=>at least one state should repeat
somewhere along the path
(by + Principle)
Uses Pigeon Hole
Principle
Rationale…
➢ ==> Let the repeating state be pi=pJ for i < j
➢ ==> We can fool the DFA by inputting 0(k-(j-i))1k
and still get it to accept (note: k-(j-i) is at most
k-1).
➢ ==> DFA accepts strings w/ unequal number of
0s and 1s, implying that the DFA is wrong!
Pumping Lemma for Regular Languages
• The Pumping Lemma for Regular Languages:
• What it is?
The Pumping Lemma is a property of all
regular languages.
• How is it used?
A technique that is used to show that a given
language is not regular
10
Pumping Lemma for Regular Languages
Let L be a regular language
Then there exists some constant N such that for
every string w L s. t. |w| ≥ N, there exists a
way to break w into three parts, w=x y z, such
that:
1. y ≠
2. | x y | ≤ N
3. For all k ≥ 0, all strings of the form x y k z L
This property should hold for all regular languages.
Definition: N is called the “Pumping Lemma Constant”
Proof of Pumping Lemma
• 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= pj where 0≤ i < j ≤ N (by pigeonhole principle (PHP))
Proof of Pumping Lemma
➢=>We should be able to break w=x y z 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
yk (for k loops)
x z
p0 pi pm
=pj
Proof of Pumping Lemma
➢Now consider another
string wk=x yk z , where k ≥ 0
➢Case k=0
➢DFA will reach the accept state pm
➢Case k>0
➢DFA will loop for yk, and finally reach the accept
state pm for z
➢In either case, wk L yk (for k loops)
p0 x pi z pm
=pj
This proves part (3) of the lemma
Proof of Pumping Lemma
• For part (1):
• Since i < j, y ≠
yk (for k loops)
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
• ==> |x y| ≤ N
Pumping Lemma
• The Purpose of the Pumping
Lemma for RL:
• To prove that some languages
cannot be regular.
Using Pumping Lemma
How to use the pumping lemma?
Think of playing a 2 person game
• Role 1: We claim that the language cannot be
regular
• Role 2: An adversary who claims the language
is regular
• We show that the adversary’s statement will
lead to a contradiction that implies pumping
lemma cannot hold for the language.
• We win!!
Using Pumping Lemma
The Steps of using pumping lemma
1.(we) L is not regular.
2.(adv.) Claims that L is regular and gives you
a value for N as its P/L constant
3.(we) Using N, choose a string w L s.t.,
1. |w| ≥ N,
2. Using w as the template, construct other words
wk of the form xykz and show that at least one
such wk L
=> this implies we have successfully broken the pumping
lemma for the language, and hence that the adversary
is wrong.
(Note: In this process, we may have to try many values of k,
starting with k=0, and then 2, 3, .. so on, until wk L )
Using the Pumping Lemma
• What WE do? • What the Adversary does?
1. Claims L is regular
2. Provides N
3. Using N, we construct
our template string w
4. Demonstrate to the
adversary, either
through pumping up or
down on w, that some
string wk L
(this should happen
regardless of w=xyz)
Note:
• We don’t have any control over N, except that it is positive.
• We also don’t have any control over how to split w=xyz,
but xyz should respect the P/L conditions (1) and (2).
Example
Using the pumping lemma to prove that a language is not regular:
Let Leq = {w | w is a binary string with equal number of 1s and 0s}
• Your Claim: Leq is not regular
• Proof:
➢ By contradiction, let Leq be regular ➔ adv.
➢ P/L constant should exist ➔ adv.
➢ Let N = that P/L constant
➢ Consider input w = 0N1N ➔ you
(your choice for the template string)
➢ By pumping lemma, we should be able to ➔you
break w=x y z, such that:
1) y ≠
2) |xy| ≤ N
3) For all k≥0, the string xykz is also in L
Note: This N can be anything (need not necessarily be the #states in the DFA.
It’s the adversary’s choice.)
Proof
Template string w = 0N1N = 00 …. 011 … 1
N N
➢ Because |x y|≤ N, xy should contain only 0s ➔ you
➢ (This and because y≠ , implies y = 0+)
➢ Therefore x can contain at most N-1 0s
➢ Also, all the N 1s must be inside z
➢ By (3), any string of the form xykz Leq for all k≥0
➢ Case k=0: xz has at most N-1 0s but has N 1s
➢ Therefore, xy0z Leq
➢ This violates the P/L (a contradiction)
Setting k=0 is referred to as Setting k>1 is referred to as
“pumping down” “pumping up”
Another way of proving this will be to show that if the #0s is
arbitrarily pumped up (e.g., k=2), then the #0s will become
exceed the #1s
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
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 N𝟐 >>>> 0 1 4 9 16 …..
➢ 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.
➢…
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.)
Example
Prove that the language of palindromes over {0, 1} is not regular.
Solution:
• Let L be a regular language and n be the integer in the
statement of the pumping lemma.
• To prove that the language is not regular we must choose a
string w of length n that will produce contradiction.
• Let us consider the string 0n 1 0n.
• Let y be of the form 0j where j > 0.
• So according to the pumping lemma 0(n-j) (0j)i1 On should be in L
for all values of i >= 0.
• But for i = 0 the string becomes O(n-j)1
• On, which is not a palindrome for any positive value of j.
• This contradicts the pumping lemma for L hence L is not a
regular language.
Example
• Prove that Language L = {anbn for n>=0} is not regular.
• Prove that the language containing strings of balanced
parentheses is not regular.
solution
• Following the same procedure as described in the above
example you can prove that the string of the form x = a(n-j) y =
aj z = bn contradicts pumping lemma for i = 0.
• The language will contain strings of the form (()()()), (((()))),
((()()())), …. Let the language be a regular language.
• Suppose we choose a string of the form (n)n.
• Now choosing x, y and z as in the above question
• we arrive at a contradiction to the pumping lemma.
• Hence the language is not regular.