DFA Formal Definition (reminder)

DFA Formal Definition (reminder)
A deterministic finite automaton (DFA) is a 5-tuple
(Q, Σ, δ, q0 , F ), where
Q is a finite set called the states,
Σ is a finite set called the alphabet,
δ : Q × Σ → Q is the transition function,
q0 ∈ Q is the start state, and
F ⊆ Q is the set of accept states.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University.
– p. 4
Languages and DFA (reminder)
Definition: Let L ( L ⊆ Σ∗ ) be the set of strings that M
accepts. L(M ), the language of a DFA M , is defined as
L(M ) = L.
Note that
M may accept many strings, but
M accepts only one language.
A language is called regular if some deterministic finite
automaton accepts it.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University.
– p. 5
Computational Models - Lecture 2
Non-Deterministic Finite Automata (NFA)
!
Closure of Regular Languages Under , ◦,
∗
Regular expressions
Equivalence with finite automata
Sipser’s book, 1.1-1.3
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University.
– p. 3
The Regular Operations (reminder)
Let A and B be languages.
The union operation:
A∪B = {x|x ∈ A or x ∈ B}
The concatenation operation:
A◦B = {xy|x ∈ A and y ∈ B}
The star operation:
A∗ = {x1 x2 . . . xk |k ≥ 0 and each xi ∈ A}
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University.
– p. 6
Claim: Closure Under Union (reminder)
If A1 and A2 are regular languages, so is A1 ∪ A2 .
Approach to Proof:
some M1 accepts A1
some M2 accepts A2
construct M that accepts A1 ∪ A2 .
in our construction, states of M were Cartesian product
of M1 and M2 states.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University.
– p. 7
What About Concatenation?
Thm:
If L1 , L2 are regular languages, so is L1 ◦ L2 .
Example: L1 = {good, bad}
and L2 = {boy, girl}.
L1 ◦ L2 = {goodboy, goodgirl, badboy, badgirl}
This is much harder to prove.
Idea:
Simulate M1 for a while, then switch to M2 .
Problem:
But when do you switch?
Seems hard to do with DFAs.
This leads us into non-determinism.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University.
– p. 8
Negative Results
We have made quite some progress understanding what
finite automata can do. But what can’t they do?
Is there a DFA that accepts
B = {0n 1n |n ≥ 0}
C = {w|w has an equal number of 0’s and 1’s}
D = {w|w has an equal number of occurrences
of 01 and 10 substrings}
Consider B :
DFA must “remember” how many 0’s it has seen
impossible with finite state.
The others are exactly the same.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University.
– p. 15
Negative Results
Is there a DFA that accepts
B = {0n 1n |n ≥ 0}
C = {w|w has an equal number of 0’s and 1’s}
D = {w|w is binary and has an equal number of
occurrences of 01 and 10 substrings}
Consider B :
DFA must “remember” how many 0’s it has seen
impossible with finite state.
The others are exactly the same...
Question: This is sound intuition. But is this a proof?
Answer: No. D is regular!???
(related to assignment 1, prob. 5d, but not identical)
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University.
– p. 16
Pumping Lemma
We will show that all regular languages have a special
property.
Suppose L is regular.
If a string in L is longer than a certain critical length !
(the pumping length),
then it can be “pumped” to a longer string by repeating
an internal substring any number of times.
The longer string must be in L too.
This is a (second) powerful technique for showing that a
language is not regular.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University.
– p. 26
Pumping Lemma
Theorem: If L is a regular language, then there is an ! > 0
(the pumping length), where if s is any string in L of length
|s| > !, then s may be divided into three pieces s = xyz such
that
for every i ≥ 0, xy i z ∈ L,
|y| > 0, and
|xy| ≤ !.
Remarks: Without the second condition, the theorem would
be trivial.
The third condition is technical and sometimes useful.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University.
– p. 27
Pumping Lemma – Proof
Let M = (Q, Σ, δ, q1 , F ) be a DFA that accepts L.
Let " = |Q|, the number of states of M .
If s ∈ L has length at least ", consider the sequence of
states M goes through as it reads s:
s1
s2
s3
s4
s5
s6
...
sn
↑
↑
↑
↑
↑
↑
↑
↑
↑
q1
q20
q9
q17
q12
q13
q9
q2
q5 ∈ F
Since the sequence of states is of length |s| + 1 > ", and
there are only " different states in Q, at least one state is
repeated (by the pigeonhole principle).
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University.
– p. 28
Pumping Lemma – Proof (cont.)
Write down s = xyz
q9
z
x
q5
y
q1
By inspection, M accepts xy k z for every k ≥ 0.
|y| > 0 because the state (q9 in figure) is repeated.
To ensure that |xy| ≤ !, pick first state repetition, which
must occure no later than ! + 1 states in sequence.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University.
– p. 29
An Application
Theorem: The language B = {0n 1n |n > 0} is not regular.
Proof: By contradiction. Suppose B is regular, accepted by
DFA M . Let ! be the pumping length.
Consider the string s = 0! 1! .
By pumping lemma s = xyz , where xy k z ∈ B for every k .
If y is all 0, then xy k z has too many 0’s.
If y is all 1, then xy k z has too many 1’s.
If y is mixed, then xy k z is not of right form.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University.
♣
– p. 30
Another Application
Theorem: The language
C = {w|w has an equal number of 0’s and 1’s} is not
regular.
Proof: By contradiction. Suppose C is regular, accepted by
DFA M . Let ! be the pumping length.
Consider the string s = 0! 1! .
By pumping lemma s = xyz , where xy k z ∈ C for every k .
If y is all 0, then xy k z has too many 0’s.
If y is all 1, then xy k z has too many 1’s.
If y is mixed, then since |xy| ≤ !, y must be all 0’s,
contradiction.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University.
♣
– p. 31
Regular Expressions
A notation for building up languages by describing them as
expressions, e.g. (0 ∪ 1)0∗ .
0 and 1 are shorthand for {0} and {1}
so (0 ∪ 1) = {0, 1}.
0∗ is shorthand for {0}∗ .
concatenation, like multiplication, is implicit, so 0∗ 10∗ is
shorthand for the set of all strings over Σ = {0, 1} having
exactly a single 1.
Q.: What does (0 ∪ 1)0∗ stand for?
Remark: Regular expressions are often
used in text editors
or shell scripts.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University.
– p. 34
More Examples
Let Σ be an alphabet.
The regular expression Σ is the language of
one-symbol strings.
Σ∗ is all strings.
Σ∗ 1 all strings ending in 1.
0Σ∗ ∪ Σ∗ 1 strings starting with 0 or ending in 1.
Just like in arithmetic, operations have precedence:
star first
concatenation next
union last
parentheses used to change default order
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University.
– p. 35
Regular Expressions – Formal Definition
Syntax: R is a regular expression if R is of form
a for some a ∈ Σ
ε
∅
(R1 ∪ R2 ) for regular expressions R1 and R2
(R1 ◦ R2 ) for regular expressions R1 and R2
(R1∗ ) for regular expression R1
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University.
– p. 36
Regular Expressions – Formal Definition
Let L(R) be the language denoted by regular expression R.
R
a
ε
(R1 ∪ R2 )
(R1 ◦ R2 )
(R1 )∗
Q.:
Q.:
L(R)
{a}
{ε}
L(R1 ) ∪ L(R2 )
L(R1 ) ◦ L(R2 )
L(R1 )∗
What’s the difference between ∅ and ε?
Isn’t this definition circular?
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University.
– p. 37
Remarkable Fact
Thm.:
A language, L, is described by a regular
expression, R, if and only if L is regular.
=⇒ construct an NFA accepting R.
⇐= Given a regular language, L, construct an
equivalent regular expression.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University.
– p. 38
Non-Deterministic Finite Automata
0,1
q
1
0,1
0
q
2
0, !
q
3
1
q
4
an NFA may have more than one transition labeled with
the same symbol,
an NFA may have no transitions labeled with a certain
symbol, and
an NFA may have transitions labeled with ε, the empty
string.
Comment:
Every DFA is also a non-deterministic finite
automata (NFA).
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University.
– p. 9
Non-Deterministic Computation
0,1
q
1
0,1
0
q
2
0, !
q
3
1
q
4
What happens when more than one transition is possible?
the machine “splits” into multiple copies
each branch follows one possibility
together, branches follow all possibilities.
If the input doesn’t appear, that branch “dies”.
Automaton accepts if some branch accepts.
What does an ε transition do?
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University.
– p. 10
Non-Deterministic Computation
0,1
q1
0,1
0
q
2
0, !
q3
1
q
4
What happens on string 1001?
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University.
– p. 11
The String 1001
0,1
q
1
symbol
1
q
1
0
q1
1
q
2
q
1
0
q
1
q
1
q
2
0,1
0
q
2
0,!
q3
1
q
4
q
3
q
3
q
3
q
4
q
4
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University.
– p. 12
Why Non-Determinism?
(to be proved soon): Deterministic and
non-deterministic finite automata accept exactly the same
set of languages.
Theorem
Q.:
So why do we need them?
A.:
NFAs are usually easier to design than equivalent DFAs.
Example:
Design a finite automaton that accepts all strings
with a 1 in their third-to-the-last position?
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University.
– p. 13
Solving with DFA
0
0
q000
1
q
001
0
0
q100
1
1
q
101
0
1
q
010
q110
0
0
q
011
1
1
q
111
1
(oops, there are three errors: q101 should be an accept
state, there should be a “0" labeled arrow from q011 to q110 ,
and the existing arrow from q110 to q101 should be labeled
“0". But overall it is OK.)
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University.
– p. 14
Solving with NFA
0,1
q
1
1
q
2
0,1
q
3
0,1
q
4
“Guesses” which symbol is third from the last, and
checks that indeed it is a 1.
If guess is premature, that branch “dies”, and no harm
occurs.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University.
– p. 15
NFA – Formal Definition
Transition function δ is going to be different.
Let P(Q) denote the powerset of Q.
Let Σε denote Σ ∪ {ε}.
A non-deterministic finite automaton is a 5-tuple
(Q, Σ, δ, q0 , F ), where
Q is a finite set called the states,
Σ is a finite set called the alphabet,
δ : Q × Σε → P(Q) is the transition function,
q0 ∈ Q is the start state, and
F ⊆ Q is the set of accept states.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University.
– p. 16
Example
0,1
q
1
0,1
0
q
2
0, !
q
3
1
q
4
N1 = (Q, Σ, δ, q1 , F )
where
Q = {q1 , q2 , q3 , q4 }, Σ = {0, 1},
δ is
q1
q2
q3
q4
0
1
ε
{q1 , q2 } {q1 }
∅
{q3 }
∅
{q3 }
∅
{q4 }
∅
{q4 }
{q4 }
∅
q1 is the start state, and F = {q4 }.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University.
– p. 17
Formal Model of Computation
Let M = (Q, Σ, δ, q0 , F ) be an NFA, and
w be a string over Σε that has the form y1 y2 · · · ym where
yi ∈ Σε .
u be the string over Σ obtained from w by omitting all
occurances of ε.
Suppose there is a sequence of states (in Q),
r0 , . . . , rn , such that
r0 = q0
ri+1 ∈δ(ri , yi+1 ), 0 ≤ i < n
rn ∈ F
Then we say that M accepts u.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University.
– p. 18
Equivalence of NFA’s and DFA’s
Given an NFA, N , we construct a DFA, M , that accepts
the same language.
To begin with, we make things easier by ignoring ε
transitions.
Make DFA simulate all possible NFA states.
As consequence of the construction, if the NFA has k
states, the DFA has 2k states (an exponential blow up).
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University.
– p. 19
Equivalence of NFA’s and DFA’s
Let N = (Q, Σ, δ, q0 , F ) be the NFA accepting A.
Construct a DFA M = (Q" , Σ, δ " , q0" , F " ).
Q" = P(Q).
For R ∈ Q" and a ∈ Σ, let
δ " (R, a) = {q ∈ Q|q ∈ δ(r, a) for some r ∈ R}
q0" = {q0 }
F " = {R ∈ Q" |R contains an accept state of N }
♣
Notice: F " is a set whose elements are subsets of Q, so
(as expected) F " is a subset of P(Q).
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University.
– p. 20
Equivalence of NFA’s and DFA’s
Formally, use induction on m to show that if y1 y2 · · · ym is a
string over Σ∗ , and the set of all possible states that
N = (Q, Σ, δ, q0 , F ) could reach on it is R ⊆ Q, then the
deterministic DFA, M , reaches state R on y1 y2 · · · ym .
Then, use the definition of acceptance by N , and of accept
states for M .
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University.
– p. 21
Dealing with ε-Transitions
For any state R of M , define E(R) to be the collection of
states reachable from R by ε transitions only.
E(R) = {q ∈ Q|q can be reached from some r ∈ R
by 0 or more ε transitions}
Define transition function:
δ " (R, a) = {q ∈ Q| there is some r ∈ R
such that q ∈ E(δ(r, a))}
Change start state to
q0" = E({q0 })
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University.
♣
– p. 22
Regular Languages, Revisited
By definition, a language is regular if it is accepted by some
DFA.
A language is regular if and only if it is accepted
by some NFA.
Corollary:
This is an alternative way of characterizing regular
languages.
We will now use the equivalence to show that regular
languages are closed under the regular operations (union,
concatenation, star).
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University.
– p. 23
Closure Under Union (alternative proof)
N1
N
2
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University.
– p. 24
Regular Languages Closed Under Union
N1
!
!
N
2
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University.
– p. 25
Regular Languages Closed Under Union
Suppose
N1 = (Q1 , Σ, δ1 , q1 , F1 ) accept L1 , and
N2 = (Q2 , Σ, δ2 , q2 , F2 ) accept L2 .
Define N = (Q, Σ, δ, q0 , F ):
Q = q0 ∪ Q1 ∪ Q2
Σ is the same, q0 is the start state
F = F1 ∪ F2


δ1 (q, a)




 δ (q, a)
2
"
δ (q, a) =

{q1 , q2 }





∅
q ∈ Q1
q ∈ Q2
q = q0 and a = ε
q = q0 and a += ε
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University.
– p. 26
Regular Languages Closed UnderConcatenation
N1
N
2
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University.
– p. 27
Regular LanguagesClosed Under Concatenation
N1
!
N
2
!
Remark: Final states are exactly those of N2 .
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University.
– p. 28
Regular LanguagesClosed Under Concatenation
Suppose
N1 = (Q1 , Σ, δ1 , q1 , F1 ) accept L1 , and
N2 = (Q2 , Σ, δ2 , q2 , F2 ) accept L2 .
Define N = (Q, Σ, δ, q1 , F2 ):
Q = Q1 ∪ Q2
q1 is the start state of N
F2 is the set of accept states of N


δ1 (q, a)
q ∈ Q1 and q ∈
/ F1




 δ (q, a)
q ∈ Q1 and a += ε
1
"
δ (q, a) =

δ1 (q, a) ∪ {q2 } q ∈ F1 and a = ε





δ2 (q, a)
q ∈ Q2
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University.
– p. 29
Regular Languages Closed Under Star
N1
N1
!
!
Oops - bad construction (really? why?).
How do we fix it?
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University.
– p. 30
Regular Languages Closed Under Star
N1 accepts R1 . Wanna build NFA for R = (R1 )∗ .
%
#
+,
)*
"#$$"
%&'(
%&'(
-./0
1234
!!%&'(
!!"#$
!!"#$
" ""6
5 !!
!
ε
+,
#
7658
=>?@
9:;<
! +,)*
q0
)*
ε
#
+,
)*
%
!%&'(
%&'(
%&'(
-./0
1234
! "#$$"
!!"#$
!!"#$
" ""6
5 !!
!
♣
Ahaa - a better construction!
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University.
– p. 31
Regular Languages Closed Under Star
Suppose N1 = (Q1 , Σ, δ1 , q1 , F1 ) accepts L1 .
Define N = (Q, Σ, δ, q0 , F ):
Q = {q0 } ∪ Q1
q0 is the new start state.
F = {q0 ∪
F1 }

δ1 (q, a)





 δ1 (q, a)
δ " (q, a) =
δ1 (q, ε) ∪ {q1 }



{q1 }



 ∅
q
q
q
q
q
∈ Q1 and q ∈
/ F1
∈ F1 and a += ε
∈ F1 and a = ε
= q0 and a = ε
= q0 and a += ε
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University.
– p. 32
Summary
Regular languages are closed under
union
concatenation
star
Non-deterministic finite automata
are equivalent to deterministic finite automata
but much easier to use in some proofs and
constructions.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University.
– p. 33
Given R, Build NFA Accepting It (=⇒)
a
1. R = a , for some a ∈ Σ
2. R = ε
3. R = ∅
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University.
– p. 39
Given R, Build NFA Accepting It (=⇒)
N1
!
!
N1
N
2
!
N
2
!
R = (R1 ∪ R2 )
R = (R1 ◦ R2 )
ε
)*
+,
R = (R1
)∗
#
7658
=>?@
9:;<
! +,)*
q0
ε
)*
#
+,
%
!%&'(
"#$
!
%&'(
"#$
!
%&'(
"#$
1234
-./0
! $"
!
!
" ""6
5 !!
!
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University.
♣
– p. 40
Approximately Correct Examples
a
a
b
b
b
!
a
b
!
a
ab
!
ab
U
a
!
a
(why
only “approximately”?)
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University.
– p. 41
Regular Expression from an NFA (⇐=)
We now define generalized non-deterministic finite
automata (GNFA).
An NFA:
Each transition labeled with a symbol or ε,
reads zero or one symbols,
takes matching transition, if any.
A GNFA:
Each transition labeled with a regular expression,
reads zero or more symbols,
takes transition whose regular expression matches
string, if any.
GNFAs are natural generalization of NFAs.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University.
– p. 42
A Special Form of GNFA
Start state has outgoing arrows to every other state, but
no incoming arrows.
Unique accept state has incoming arrows from every
other state, but no outgoing arrows.
Except for start and accept states, arrows goes from
every state to every other state, including itself.
Easy to transform any GNFA into special form.
Really? How? . . .
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University.
– p. 43
Converting DFA to Regular Expression (⇐=)
Strategy – sequence of equivalent transformations
given a k -state DFA
transform into (k + 2)-state GNFA
while GNFA has more than 2 states, transform it into
equivalent GNFA with one fewer state
eventually reach 2-state GNFA (states are just start and
accept).
label on single transition is the desired regular
expression.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University.
♠
– p. 44
Converting Strategy (⇐=)
3-state
DFA
5-state
GNFA
4-state
GNFA
3-state
GNFA
2-state
GNFA
regular
expression
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University.
– p. 45
Removing One State
We remove one state qr , and then repair the machine by
altering regular expression of other transitions.
R
4
q
i
qj
R1
R3
qr
R2
q
i
R1R*2R3 U R4
qj
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University.
– p. 46
Formal Treatment – GNDA Definition
qs is start state.
qa is accept state.
R is collection of regular expressions over Σ.
The transition function is
δ : (Q − {qa }) × (Q − {qs }) → R
If δ(qi , qj ) = R, then arrow from qi to qj has label R.
Arrows connect every state to every other state except:
no arrow from qa
no arrow to qs
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University.
– p. 47
Formal Definition
A generalized deterministic finite automaton (GNFA) is
(Q, Σ, δ, qs , qa ), where
Q is a finite set of states,
Σ is the alphabet,
δ : (Q − {qa }) × (Q − {qs }) → R is the transition function.
qs ∈ Q is the start state, and
qa ∈ Q is the unique accept state.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University.
– p. 48
A Formal Model of GNFA Computation
A GNFA accepts a string w ∈ Σ∗ if there exists a parsing of
w, w = w1 w2 · · · wk , where each wi ∈ Σ∗ , and there exists a
sequence of states q0 , . . . , qk such that
q0 = qs , the start state,
qk = qa , the accept state, and
for each i, wi ∈ L(Ri ), where Ri = δ(qi−1 , qi ).
(namely wi is an element of the language described by
the regular expression Ri .)
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University.
– p. 49
The CONVERT Algorithm
Given GNFA G, convert it to equivalent GNFA G! .
let k be the number of states of G.
If k = 2, return the regular expression labeling the only
arrow.
If k > 2, select any qr distinct from qs and qa .
Let Q! = Q − {qr } .
For any qi ∈ Q! − {qa } and qj ∈ Q! − {qs }, let
R1 = δ(qi , qr ), R2 = δ(qr , qr ),
R3 = δ(qr , qj ), and R4 = δ(qi , qj ).
Define δ ! (qi , qj ) = (R1 )(R2 )∗ (R3 ) ∪ (R4 ).
Denote the resulting k − 1 states GNFA by G! .
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University.
– p. 50
The CONVERT Procedure
We define the recursive procedure CONVERT(·):
Given GNFA G.
Let k be the number of states of G.
If k = 2, return the regular expression labeling the only
arrow of G.
If k > 2, let G! be the k − 1 states GNFA produced by
the algorithm.
Return CONVERT(G! ).
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University.
– p. 51
Conversion - Example
We now construct a simple, 2 state DFA that accepts
the language over {0, 1} of all strings with an even
number of 1s.
We followed the conversion through GNFAs to translate
this DFA (on the blackboard) into the regular expression
(0 ∪ 10∗ 1)∗ .
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University.
– p. 52
Correctness Proof of Construction
Theorem: G and CONVERT(G) accept the same language.
Proof: By induction on number of states of G
Basis: When there are only 2 states, there is a single label,
which characterizes the strings accepted by G.
Induction Step: Assume claim for k − 1 states, prove for k .
Let G! be the k − 1 states GNFA produced from G by the
algorithm.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University.
– p. 53
G and G! accept the same language
By the induction hypothesis, G! and CONVERT(G! ) accept
the same language.
On input G, the procedure returns CONVERT(G! ).
So to complete the proof, it suffices to show that G and G!
accept the same language.
Three steps:
1. If G accepts the string w, then so does G! .
2. If G! accepts the string w, then so does G.
3. Therefore G and G! are equivalent.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University.
– p. 54
Step One
Claim: If G accepts w, then so does G! :
If G accepts w, then there exists a “path of states”
qs , q1 , q2 , . . . , qa traversed by G on w , leading to the
accept state qa .
If qr does not appear on path, then G! accepts w
because the the new regular expression on each edge
of G! contains the old regular expression in the “union
part”.
If qr does appear, consider the regular expression
corresponding to . . . qi , qr , . . . , qr , qj . . . .
The new regular expression (Ri,r )(Rr,r )∗ (Rr,j ) linking qi
and qj encompasses any such string.
In both cases, the claim holds.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University.
– p. 55
Steps Two and Three
Claim: If G! accepts w, then so does G.
Proof: Each transition from qi to qj in G! corresponds to a
transition in G, either directly or through qr . Thus if G!
accepts w, then so does G.
This completes the proof of the claim that
L(G) = L(G! ).
Combined with the induction hypothesis, this shows that
G and the regular expression CONVERT(G) accept the
same language.
This, in turn, proves our remarkable claim:
A language, L, is described by a regular expression, R,
if and only if L is regular.
♣
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University.
– p. 56
What We Just Completed
Thm.: A language, L, is described by a regular
expression, R, if and only if L is regular.
=⇒ construct an NFA accepting R.
⇐= Given a regular language, L, construct an
equivalent regular expression
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University.
– p. 14
Algorithmic Questions for NFAs
Q.: Given an NFA, N , and a string s, is s ∈ L(N )?
Answer: Construct the DFA equivalent to N and run it on w.
Q.: Is L(N ) = ∅?
Answer: This is a reachability question in graphs: Is there a
path in the states’ graph of N from the start state to some
accepting state. There are simple, efficient algorithms for
this task.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University.
– p. 34
More Algorithmic Questions for NFAs
Q.: Is L(N ) = Σ∗ ?
Answer: Check if L(N ) = ∅.
Q.: Given N1 and N2 , is L(N1 ) ⊆ L(N2 )?
Answer: Check if L(N2 ) ∩ L(N1 ) = ∅.
Q.: Given N1 and N2 , is L(N1 ) = L(N2 )?
Answer: Check if L(N1 ) ⊆ L(N2 ) and L(N2 ) ⊆ L(N1 ).
In the future, we will see that for stronger models of
computations, many of these problems cannot be solved by
any algorithm.
Slides modified by Benny Chor, based on original slides by Maurice Herlihy, Brown University.
– p. 35