0% found this document useful (0 votes)
10 views38 pages

35 MG

Uploaded by

Phương Lê Thu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views38 pages

35 MG

Uploaded by

Phương Lê Thu
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 38

COMPSCI 250: Introduction

to Computation

Lecture #35: λ-NFA’s From Regular Expressions


David Mix Barrington and Mordecai Golin
1 May 2024
λ-NFA’s From Regular Expressions
• (Last three slides of Lecture #34)
• Review: Parts of Kleene’s Theorem
• Review: Induction on Regular Expressions
• A Normal Form for λ-NFA’s
• The Construction
• The Star Case and the Proof
• An Example: (ab + ba)* + bb
• Taking This Example to a Minimal DFA
2
Another Example
• Here is a 𝜆-NFA with 𝑎
state set {𝑝, 𝑞, 𝑟} 𝑝 𝑞
start state 𝑝, 𝜆 𝜆
final state set {𝑞},
input alphabet {𝑎, 𝑏} and 𝑏
𝑟
Δ = 𝑝, 𝑎, 𝑞 , 𝑝, 𝜆, 𝑟 , 𝑟, 𝜆, 𝑞 , 𝑟, 𝑏, 𝑟
(We’ve reversed the two λ-moves from before.)

𝜆
• There are two-letter moves and 𝑝 𝑞
two 𝜆-moves. For the transitive
𝜆 𝜆
closure we must add one more
move 𝑝, 𝜆, 𝑞 .
𝑟

3
Another Example
𝑎
• The 𝜆-NFA pretty clearly has 𝑝 𝑞

language 𝑎 + 𝑏 . Making an
𝜆
ordinary NFA for this language 𝜆

might be harder than making 𝑏


this one. 𝑟

𝑎, 𝑏
• By the construction, 𝑝 𝑞
the 𝑎-move makes only itself;
𝑏
the 𝑏-move makes four 𝑏-moves 𝑏

in all: 𝑏
𝑟
𝑝, 𝑏, 𝑟 , 𝑝, 𝑏, 𝑞 , 𝑟, 𝑏, 𝑟 , 𝑟, 𝑏, 𝑞
4
Another Example
• The start state changes to final 𝑎
since 𝜆 was in the language of 𝑝 𝑞

the 𝜆-NFA. 𝜆 𝜆

𝑏
𝑟
• Looking at the ordinary NFA,
we might come up with the
regular expression 𝑎, 𝑏

𝜆 + 𝑎 + 𝑏 + 𝑏𝑏 𝑏 𝑝 𝑞

but this is equivalent to 𝑎 + 𝑏 . 𝑏 𝑏
The Subset Construction gives
a 4-state DFA from this NFA. 𝑟
𝑏

5
Review: Kleene’s Theorem Parts
• Our goal in Kleene’s Theorem is to convert
regular expressions to DFA’s and vice versa.
• In the last two lectures we’ve provided two of
the three pieces of the transformation from
regular expressions to DFA’s.
• We defined ordinary NFA’s and λ-NFA’s,
then presented the Subset Construction to
turn ordinary NFA’s to DFA’s, then
presented the Killing λ-Moves Construction to
turn λ-NFA’s to ordinary NFA’s.

6
Kleene’s Theorem Chart

Fri 3
R.E.-NFA Fri 3

Reg. Expr. DFA

Today Fri 26

λ-NFA Ord. NFA


Mon 29

7
Kleene’s Theorem Parts
• Today we will see how to convert regular
expressions to equivalent 𝜆-NFA’s.
This will complete the steps needed to go
from regular expressions to DFA’s.

• In our next lecture we will finish


Kleene’s Theorem by presenting the
State Elimination Construction
to convert DFA’s (or NFA’s, or 𝜆 -NFA’s)
to regular expressions.
8
Review: Induction on R.E.’s
• We want to prove that for every regular expression 𝑅,
we can construct a 𝜆-NFA 𝑁 such that 𝐿 𝑁 = 𝐿 𝑅 .
• The way to prove a proposition 𝑃(𝑅) for all regular
expressions 𝑅 is to use induction on the definition of
regular expressions.
We must first prove the two base cases,
(1) 𝑃 ∅ and (2) 𝑃(𝑎) for every letter 𝑎 ∈ Σ.
Then we must prove the three inductive cases.
If 𝑃(𝑅) and 𝑃(𝑆) are true, we must
(3) prove 𝑃(𝑅 + 𝑆)
(4) prove 𝑃(𝑅𝑆) and

(5) prove 𝑃 𝑅 . 9
Review: Induction on R.E.’s
• Here 𝑃(𝑅) is “there exists 𝑁 with 𝐿 𝑁 = 𝐿 𝑅 ”.

• As with our other inductive proofs on regular


expressions, we will actually define a recursive
algorithm that will take 𝑅 as input and return 𝑁
as output.

• We could code this algorithm in pseudo-Java


using the class definition for RegExp and a new
class definition for LambdaNFA, but we will stick
with an informal description here.

10
A Normal Form for λ-NFA’s
• Since we want to actually carry out this
construction by hand on examples, we’re going
to make it a little more complicated than it
would need to be just to prove that a valid
construction exists.

• We’ll produce 𝜆-NFA’s in a particular normal form


-- they will satisfy three rules that will allow us to
make simpler 𝜆 -NFA’s in most cases.

11
A Normal Form for λ-NFA’s
Rule (1): the λ-NFA has exactly one final state,
which isn’t the start state.

Rule (2): no transitions go into the start state.

Rule (3): no transitions go out of the final state.


possible but not required

no final state

Similar rules will also show up later in the State


Elimination Construction.
12
The Construction
(1) For ∅, we need a 𝜆-NFA with a start state and a
final state. That’s all we need -- if it has no
transitions, it accepts no strings and its language is ∅.

(2) For 𝑎, we can again have a start state 𝑖 and a


final state 𝑓, with a single transition (𝑖, 𝑎, 𝑓). The
rules are satisfied, and the language is 𝑎 , as it
should be.

(1) (2)
13
The Construction
3) Now assume, as our IH, that we have
constructed 𝜆-NFA’s 𝑀 and 𝑀′ for our two
regular expressions 𝑅 and 𝑅′, and
that M and Mʹ follow the three rules.
We need to build new 𝜆 -NFA 𝑀′′ satisfying
𝐿 𝑀"" = 𝐿 𝑀 ∪ 𝐿 𝑀" = 𝐿(𝑅 + 𝑅 " )

𝑀′′ will contain (copies of) all the states of 𝑀


and 𝑀′, but we will merge their two initial
states, and merge their two final states.
14
Step (3) Example
!!
𝑀
𝑎
𝑀 𝑏 𝑎 𝑏

𝑏 𝑎
𝑏 𝑎
𝑀′

𝐿 𝑀 =𝐿 𝑅 𝐿 𝑀"" = 𝐿 𝑀 ∪ 𝐿 𝑀"
𝐿 𝑀′ = 𝐿 𝑅′ = 𝐿(𝑅 + 𝑅 " )

15
Step (4) and Example
𝑀 !!
𝑎 𝑏 𝑀
𝑠! 𝑚! 𝑓! 𝑎 𝑏 𝑏 𝑎
𝑠 𝑚! 𝑚! = 𝑓" = 𝑠# 𝑚" 𝑓
𝑏 𝑎
𝑠" 𝑚" 𝑓"

𝑀′
"""
(4) To make 𝑀 with
𝐿 𝑀""" = 𝐿 𝑀 𝐿 𝑀" = 𝐿 𝑅𝑅 " ,
we instead merge the final state of 𝑀
with the initial state of 𝑀′,
making the new state non-final.
16
Clicker Question #1
With the steps of the construction so far, we
can make a λ-NFA for the regular expression
(aa + b)ab(a + bab).
How many states does it have?
(a) 6
(b) 7
(c) 8
(d) 10
17
Not the Answer

18
Clicker Answer #1
With the steps of the construction so far, we
can make a λ-NFA for the regular expression
(aa + b)ab(a + bab).
How many states does it have?
(a) 6 a a a b a

(b) 7 b b
b
a
(c) 8 string: len + 1 states
union: sum - 2; concat: sum - 1
(d) 10 (aa+b): 3 + 2 - 2 = 3 states, (a+bab) = 4
Total: 3 + 3 + 4 - 2 = 8 states
19
Step (5): The Star Case
(5) Finally we want to build a λ-NFA N such
∗ ∗
that 𝐿 𝑁 = 𝐿 𝑀 = 𝐿 𝑅
Assume that M has start state 𝑖 and final state 𝑓.
𝑁’s states will be 𝑀’s states plus two more;
a new start state 𝑠 and a new final state 𝑡.

We then add four new λ-moves:


𝑠, 𝜆, 𝑖 , 𝑖, 𝜆, 𝑓 , 𝑓, 𝜆, 𝑖 , and 𝑓, 𝜆, 𝑡

We then make 𝑓a non-final state.

20
Step (5) Example
𝑎 𝑏
𝐿(𝑀) = 𝐿(𝑅) 𝑓
𝑖

𝜆
𝐿(𝑀′′) = 𝐿(𝑅∗ ) 𝑎 𝑏 𝜆
𝑠 𝑖 𝑓 𝑡

21
Validity of the Construction

• Now we want to prove by induction on all


regular expressions that this construction is
correct -- if N is the 𝜆-NFA made from 𝑅,
then 𝐿 𝑁 = 𝐿(𝑅).
• This is pretty obvious for the two base cases
as we can check the languages of the 𝜆 -
NFA’s directly.
• So we must check the three inductive cases.

22
Validity of the Construction
• With the two 𝜆 -NFA’s connected in parallel in step (3), a path
from the start to final state of 𝑀"" must go through either only
states of 𝑀 or only states of 𝑀" .
• The first move must be into either one machine or the other.
From then on we stay in that machine until we finish, as the
rules stop us from returning to the start or continuing past the
final state.
• The path has to either read a string in 𝐿 𝑀
or read a string in 𝐿 𝑀′ , so it is 𝐿 𝑀 ∪ 𝐿 𝑀" .

𝑎 𝑏 𝑎 𝑏

𝑏 𝑎 𝑏 𝑎

23
Correctness of Step (4)
• In step (4) we created 𝑀""" by connecting 𝑀 and 𝑀′ in series,
and we must show that 𝐿 𝑀""" = 𝐿 𝑀 𝐿 𝑀" .
How could a path get from the start state of 𝑀"""
(which is the start state of 𝑀) to
the final state of 𝑀""" (which is the start state of 𝑀" )?

• The first transition has to be in 𝑀, then the path must stay in 𝑀


until it reaches the final state of 𝑀. The only way out of that
state is into 𝑀′, where it must stay until it reaches the final state
and then stops. So the path reads a string in 𝐿(𝑀) followed by a
string in 𝐿(𝑀" ), as it should.

𝑎 𝑏
𝑠! 𝑚! 𝑓! 𝑎 𝑏 𝑏 𝑎
𝑠 𝑚! 𝑚! = 𝑓" = 𝑠# 𝑚" 𝑓
𝑏 𝑎
𝑠" 𝑚" 𝑓"
24
Correctness of Step (5)
In step (5) we created 𝑁 by adding two new states and four new
λ-moves to 𝑁. First note that we can read any sequence of zero
or more strings in 𝐿(𝑀) by going to 𝑖, reading each string going
from 𝑖 to 𝑓, returning to 𝑖 each time, then winding up in 𝑡.

Furthermore, any path from 𝑠 to 𝑡 must consist of some


combination of trips from 𝑖 through 𝑀 to 𝑓, and uses of the new
λ-moves. So the string we read is the concatenation of zero or
more strings in 𝐿(𝑀), and thus is in 𝐿 𝑀 ∗ .

𝜆
𝑎 𝑏 𝜆 𝑎 𝑏 𝜆
𝑖 𝑓 𝑠 𝑖 𝑓 𝑡
𝐿(𝑀) = 𝐿(𝑅) ∗ 𝜆
𝐿(𝑀′′) = 𝐿(𝑅 )
25
Notes on the Construction
• The construction makes use of the normal form
constantly -- if we could not assume that the input
𝜆-NFA’s followed the rules, we would need to
introduce new states and new 𝜆 -moves in steps
(3) and (4) as well as in (5).

• We pay for the normal form in step (5). We need


to connect the start and final states, but then, in
order to obey the rules, we need to put in new
start and final states.

26
Clicker Question #2
Let M be a λ-NFA obeying (1), (2), and (3), with start
and final states i and f. We form Mʹ by adding a λ-
move from f to i. Is L(Mʹ) = L(M)*?
(a) Yes, always, but Mʹ breaks our normal form rules.
(b) Yes, but only if λ was already in L(M).
(c) No, never, because M' now accepts λ.
(d) No, never, and Mʹ breaks the normal form rules.
λ

i M f i M f
27
Not the Answer

28
Clicker Question #2
Let M be a λ-NFA obeying (1), (2), and (3), with start
and final states i and f. We form Mʹ by adding a λ-
move from f to i. Is L(Mʹ) = L(M)*?
(a) Yes, always, but Mʹ breaks our normal form rules.
(b) Yes, but only if λ was already in L(M).
(c) No, never, because M' now accepts λ.
(d) No, never, and Mʹ breaks the normal form rules.
λ

i M f i M f
29
Notes on the Construction
• We only create 𝜆-moves when we do step (5).
Thus if R has few or no stars, we will get a λ-NFA
with few or no 𝜆 -moves, which can be good because
making an ordinary NFA is more complicated, the
more 𝜆 -moves there are.

• We can sometimes see ways to simplify the 𝜆 -NFA


without changing the language. But we need to be
careful that our simplification is correct.

• It can be shown that the number of states in the 𝜆 -


NFA is about the same as the length of the regular
expression. So the only big blowup is NFA’s to DFA’s.
30
An Example: (ab + ba) * + bb
• Let’s see how the construction works on a fairly
complicated regular expression.
(There are diagrams of this example in the text.)
We can do the construction either top-down or
bottom-up -- let’s try bottom-up.

• The three regular expressions “𝑎𝑏”, “𝑏𝑎”, and “𝑏𝑏”


each get three-state λ-NFA’s, with letter moves
from the start state to a middle state and from that
middle state to a final state.

𝑎 𝑏 𝑏 𝑎 𝑏 𝑏

31
An Example: (ab + ba)* + bb
As we saw, the 𝜆-NFA for “𝑎𝑏 + 𝑏𝑎” has four states,
three each for “𝑎𝑏” and “𝑏𝑎” minus two when we
merge the two start states and two final states.

To get a λ-NFA for 𝑎𝑏 + 𝑏𝑎 ∗ we add a new start and


final state, plus four new 𝜆 -moves, to get a six-state 𝜆 -
NFA with four letter moves and four 𝜆 -moves.

𝜆
𝑎 𝑏 𝑎 𝑏
𝜆 𝜆

𝑏 𝑎 𝑏 𝑎
𝜆
32
An Example: (ab + ba)* + bb
• To conclude, we place this six-state machine
in parallel with the three-state machine for
“𝑏𝑏”, getting a seven-state machine with six
letter moves and four 𝜆-moves.

𝑟
𝑏 𝑏
𝜆
𝑎 𝑠 𝑏
𝜆 𝜆
𝑖 𝑝 𝑞 𝑓
𝑏 𝑎
𝑡 𝜆

33
Killing the λ-Moves
• Killing the λ-moves in this seven-state 𝜆-NFA
gives us a seven-state ordinary NFA with state
set 𝑖, 𝑝, 𝑞, 𝑟, 𝑠, 𝑡, 𝑓 , start state 𝑖, final state set
{𝑖, 𝑓}, and fourteen transitions:

𝑡 𝑡
𝑏 𝑏 𝑏 𝑏
𝜆
𝑎 𝑏
𝑞 𝑏 𝑎 𝑎 𝑞
𝜆 𝜆 𝑏 𝑎 𝑏
𝑝 𝑠 𝑓
𝑖 𝑝 𝑠 𝑓 𝑖 𝑎 𝑏
𝑏 𝑎 𝑏 𝑏 𝑎 𝑎
𝑟 𝜆 𝑟
34
Making a DFA
We could potentially get 128 states in our DFA,
but fortunately the process stops with only

seven. This DFA for 𝑎𝑏 + 𝑏𝑎 + 𝑏𝑏 happens to
be minimal.

{𝑞} 𝑎, 𝑏
𝑎 𝑏 {𝑟}
𝑡
∅ 𝑎 𝑏
𝑏 𝑏 𝑎 𝑏
𝑎 𝑎, 𝑏
𝑏
𝑎 𝑎 𝑞 𝑎 {𝑝, 𝑠, 𝑓} {𝑓}
𝑏 𝑎 𝑏 {𝑖}
𝑝 𝑠 𝑏 𝑏
𝑎 𝑓
𝑖 𝑏
𝑏 𝑏 𝑎 𝑎
𝑟 {𝑟, 𝑡}

35
Clicker Question #3
• Which of these statements
about minimizing this DFA is
true?
• (a) States {q} and {r} are a,b
distinguished. {r}
{q} a b
• (b) We may merge {r} and ∅. ∅
a b a
a b a,b
• (c) We may merge {f} and ∅.
{i} a {p,s,f} {f}
• (d) On our first pass through b
the algorithm, we will split the
{r,t} b
final states.
36
Not the Answer

37
Clicker Question #3
• Which of these statements
about minimizing this DFA is
true?
• (a) States {q} and {r} are a,b {r}
{q} a b
distinguished. By b.

• (b) We may merge {r} and ∅. a b a
a b a,b
Distinguished by a.
• (c) We may merge {f} and ∅. {i} {p,s,f} {f}
a
One final, one nonfinal.
• (d) On our first pass, we will b
split the final states. {r,t} b
Both go to nonfinal on both.
38

You might also like