Extra On Regular Languages and Non-Regular Languages
Extra On Regular Languages and Non-Regular Languages
Non-Regular Languages
IT 422
Decision Properties of Regular
Languages
• Given a (representation, e.g., RE, FA, of a)
regular language L, what can we tell about L?
• Since there are algorithms to convert between
any two representations, we can choose the
representation that makes whatever test we are
interested in easiest.
Decision Properties
• Membership: Is string w in regular language L?
– Choose DFA representation for L.
– Simulate the DFA on input w.
• Emptiness: Is L = Ø?
– Use DFA representation for L
– Use a graph-reachability algorithm (e.g. Breadth-First-
Search or Depth-First-Search or Shortest-Path) to test if
at least one accepting state is reachable from the start
state. If so, the language is not empty. If we can’t
reach an accepting state, then the language is empty.
Decision Properties
• Finiteness: Is L a finite language?
– Note that every finite language is regular (why?), but a
regular language is not necessarily finite.
– DFA method:
• Given a DFA for L, eliminate all states that are not reachable
from the start state and all states that do not reach an accepting
state.
• Test if there are any cycles in the remaining DFA; if so, L is
infinite, if not, then L is finite. To test for cycles, we can use
a Depth-First-Search algorithm. If in the search we ever visit a
state that we’ve already seen, then there is a cycle.
Decision Properties
• Equivalence: Do two descriptions of a language actually
describe the same language? If so, the languages are
called equivalent.
– For example, we’ve seen many different (and sometimes complex)
regular expressions that can describe the same language. How can
we tell if two such expressions are actually equivalent?
• To test for equivalence our strategy will be as follows:
– Use the DFA representation for the two languages
– Minimize each DFA to the minimum number of needed states
– If equivalent, the minimized DFA’s will be structurally identical
(i.e. there may be different names for the states, but all transitions
will go to identical counterparts in each DFA).
Minimization
• To test for equivalence on the previous slide, we need some method to
minimize a DFA (not in textbook)
• We say that two states p and q in a DFA are equivalent if:
– For all input strings w, following the path for w from state q is an accepting
state if and only if following the path for w from q is an accepting state.
– i.e. if we have reached state p or q, then any other string we might get that will
lead to an accepting state from p will also lead to an accepting state from q.
– Also any string we get that does not lead to an accepting state from p also does
not lead to an accepting state from q.
– If this condition holds, then p and q are equivalent. If this condition does not
hold, then p and q are distinguishable.
• The simplest case of a distinguishable pair is an accepting state p and a non-
accepting state q. In this case, δ(p, ε) is an accepting state, but δ(q, ε) is not
accepting. Therefore, any pair of (accepting, non-accepting) states are
distinguishable.
Algorithm to Discover
Distinguishable Pairs
1. Given an automaton A that has states q0…qn make a diagonal table
with labels 1 to n on the rows and 0 to n-1 on the columns.
2. Initialize the table by placing X’s for each pair that we know is
distinguishable. Initially, this is any pair of accepting and non-
accepting states.
3. Let p and q be states such that for some input symbol a, r = δ(p, a)
and s = δ(q, a). If the pair (r,s) is known to be distinguishable (i.e.
they have an X in their table entry) then the pair p and q are also
distinguishable. Place an X for (p,q) in the table.
4. Repeat step 3 until all pairs have been examined.
5. Repeat steps 3-4 again for any empty entries in the table. If no new
X’s can be placed, then the algorithm is complete. Any entries in
the table without an X are pairs of states that are equivalent.
Once the equivalent states have been identified, they can be combined into a
single state to make a new, minimized automaton. DFA’s have unique
minimum-state equivalents.
Simple Minimization Example
• Minimize the following DFA
(q,r) distinguishable?
On“X”
0: both to p,distinguishable
Known not distinguishable
states
0 Will never distinguish (q,r), states equiv!
On 1: to (q,r), no X,
Create so leave alone
Table:
0
Start p q q X
1
1 1
r X
0
r p q
Simple Minimization Example
• Combine q,r:
0 0
Start p q
0,1
1 Start p qr
1 1
0 1
r
Another Minimization Example
• Minimize the following:
0 1
1 0
0
Start A B C D
0 1
1
0 1
1 1 0
E F G H
1 0
0
Initial Table - Minimization
B
C X X
D X
E X
F X
G X
H X
A B C D E F G
Table – After First Iteration
B X
C X X
D X X X
E X X X
F X X X X
G X X X X X
H X X X X X X
A B C D E F G
Table – Last Iteration
B X
C X X
D X X X
E X X X
F X X X X
G X X X X X X
H X X X X X X
A B C D E F G
0 1
0 1
Start AE BH C
0
1
1
DF G
1 0
Proving Languages Not Regular
• Before we show how languages can be
proven not regular, first, how would we
show a language is regular?
• Regular languages and automata seem
powerful – after all they model everything
we have seen so far!
– But there are many simple examples that are
not regular languages
The Pumping Lemma
• Our strategy for proving languages to be non-
regular:
– The Pumping Lemma states that all regular languages
have a special property
• If we can show that a language does not have this property,
then the language cannot be regular.
• The property states that all strings in a regular language can be
“pumped” if they are at least as long as a special value, the
pumping length.
• This means that each such string contains a section that can be
repeated any number of times with the resulting string
remaining in the language.
The Pumping Lemma
• Let L be a regular language. Then there is a
number p (the pumping length) where, if s is
any string in L of length at least p, then s may
be divided into three parts, s=xyz, satisfying the
following conditions:
1. y ε (but x and z may be ε)
2. |xy| p
3. for each i 0, xyiz L
Pumping Lemma Explanation
• This theorem says:
– when s is divided into xyz, either x or z may be empty,
but y may not be empty.
– x and y together must have length at most p.
– we can always find a nonempty string y not too far
from the beginning of s that can be “pumped”, i.e. it
can repeat any number of times.
• Note that although yε, for the third condition, i
may equal zero, giving us y0=ε, and then xz must
be L.
Pumping Lemma Proof
• First, consider a simple case of a regular language L
– In this language there are no strings of length at least p
– In this case, the theorem becomes vacuously true.
• The three conditions hold for all strings of length at least p if
there aren’t any such strings.
• For example, if L is composed of simply the finite set { a }, then
we could pick p=2 and the theorem is vacuously true because
there are no strings of length at least 2.
– This implies for any finite set of strings, the language is regular
since we can pick a value p larger than the biggest string in L
Pumping Lemma
• More general case:
– Suppose that L is regular. Then L=L(A) for some
DFA A.
– Suppose that A has n states.
– Consider any string s of length n or more; say
s=a1a2…am where m n.
• The DFA is in some state, defined as pi, after reading
the first i symbols of s.
• By the pigeonhole principle, when we process input
symbol i, since there are more (or equal) input symbols
then there are states, we must repeat some state more
than once!
More Pumping Lemma
• We must repeat some state more than once
– Thus we can find two different integers i and j with 0
i < j n, such that pi = pj. We can break s into s=xyz as
follows:
1. x = a1a2…ai
2. y = ai+1ai+2…aj
3. z = aj+1aj+2…am
– i.e., string x takes us to state pi. Then we somehow
repeat back to that state; at this point we are at input
symbol j, and the corresponding state is pj (where pi =
pj). Finally we finish by moving to an accepting state.
– Since i is less than j (not less than or equal to j) this
means that we must have at least one symbol for y, so y
may not be empty
Pumping Lemma Illustration
• The following automaton illustrates the
pumping lemma
y=ai+1…aj
x=a1…ai z=aj+1…am
Start p0 pi
If this behavior is possible, then it means that xy*z must be in the language.
Using the Pumping Lemma
• To show that L is not regular, first assume that L is regular
in order to obtain a contradiction.
• Then use the pumping lemma to guarantee the existence of
a pumping length p such that all strings of length at least p
can be pumped
• Find a string s in L that has length p or greater but cannot
be pumped
– This is demonstrated by considering all ways of dividing s into
x,y, and z and showing that condition 3 is violated
• Since the existence of s contradicts the pumping lemma if
L was regular means that L is not regular.
Pumping Lemma Proofs
• Tricky part - coming up with the string s.
– This may take some creative thinking and trial
and error, because for a non-regular language,
some strings may fit the pumping lemma
conditions, while others won’t.
– If a string works, you may need to pick another
one until you find one that leads to the
contradiction.
Example 1
• Let B be the language {0n1n | n 0}. Use the
pumping lemma to show that this is not regular.
– Intuitively – not regular if you can’t build a DFA for it
• Assume that B is regular.
– Let p be the pumping length.
– Choose s to be the string 0p1p. This string is clearly a
member of B.
– s has length at least p, so it is a valid choice.
For s = xyz, |s| p
1. y ε
2. |xy| p
3. for each i 0, xyiz L
Example 1, Continued, 0 1 n n