0% found this document useful (0 votes)
9 views

Thompson Algorithm

thompson
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Thompson Algorithm

thompson
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

CS/ECE 374 B Lab 3½ — Solutions Fall 2019

1. Use Thompson’s algorithm to create an NFA for the following regular expressions:

(a) (0 + 1)∗

1
ϵ 1/s 1/a
ϵ ϵ
(0+1)*/s 0+1/s
ϵ
0 ϵ
0/s 0/a 0+1/a

(b) 01∗ + 10∗

ϵ ϵ 1/s 1 1/a
0/s 0 0/a 1*/s
ϵ ϵ

01*+10*/s 01*+10*/a
ϵ ϵ

1 ϵ ϵ
1'/s 1'/a 0*/s
ϵ

0'/s 1 0'/a

1
CS/ECE 374 B Lab 3½ — Solutions Fall 2019
active thread in a particular state. Thus, to simulate the NFA, the DFA only needs to maintain the current
set of states of the NFA.
+ ε)∗ 1∗ (0 +based
(c) (0construction
The formal ε)∗ on the above idea is as follows. Consider an NFA N = (Q, , , s, A).
Define the DFA det(N ) = (Q , , , s , A ) as follows.
ϵ
• Q = P(Q)
• s = N (s, ) ϵ
ϵ/s ϵ ϵ/a ϵ 0+ϵ/a
ϵ ϵ
(0+ϵ)*/s 0+ϵ/s
ϵ
• A = {X Q|X A= } 0/s
0
0/a
ϵ

• ({q1 , q2 , . . . qk }, a) = N (q1 , a) N (q2 , a) ··· N (qk , a) or more concisely, ϵ


1*/s ϵ 1 ϵ ϵ
1/s 1/a 1*/a (0+ϵ)*'/s
ϵ
(X, a) = N (q, a) 0+ϵ'/s ϵ ϵ'/s ϵ ϵ'/a ϵ 0+ϵ'/a
Induction Hypothesis Assume inductively thatq the
X
statement holds w. |w| < n. ϵ ϵ

0
Induction Step Let w be such that |w| = n (for n > 0). Without loss of generality w 0'/s
is of the0'/aform ua
An example NFA
with is
|u|shown
= n in Figure
1 and a 4 along
. with the DFA det(N ) in Figure 5.

det(N ) (s , ua) =
0,property
1 det(N ) ( det(N ) (s, u), a)
of of DFAs
0, 1
=
property of ( det(N ) (s, u), a)
of DFAs
Example q det(N )
2. Use the incremental subset construction to build aa)DFA {q
that
=
definition of
accepts the same
(s,u) N (q, a)
= q
N (s,u)
N (q, induction
1} {}on ulanguage as
hypothesis
the following NFAs: = N (s, ua) Proposition 1

(a)
No ✏-transitions
0, 1
0
3.1 0, 1
Relevant 0, 1
States
The formal definition of the DFA has many states, several of which 1
{q0 } are unreachable
{q0 ,from
q1 } the initial state
(see Figure 5).
q0 To 1
make the
q1 algorithm simpler for a human to implement (and for the resulting DFA to be
readable), one can include only the reachable states. To do this,

1. Start by drawing the initial state of the DFA.


Incremental construction
Solution:
Figure 4: NFA N Figure 5: DFA det(N ) equivalent to N
2. While there are states with missing transitions, draw the missing transitions creating any new states
that maybe needed.
We will now prove that the DFA defined above is correct. That is
3. Step 2 is repeated until transitions from every state0,1has been drawn.
Lemma 4. L(N ) = L(det(N )) 0
0
Only build states reachable from s = ✏reach(s) the start state of M
4. Figure out which states are final, and mark them appropriately.
Proof. Need to show
1 w iff{Nq0,accepts
The method of addingwonly relevant
. det(N }) accepts
{ q0 states is shown q1 } 6wand 7.
in Figures
w . det(N ) (s , w) A iff N (s, w) A =
w . det(N ) (s , w) A = iff N (s, w) A = 0, 1

Again for the induction proof to go through we need to strengthen the claim as follows.
0, 1
(b) w . det(N ) (s , w) = N (s, w) {q3 } {}

In other words, this says that the state of the DFA after reading some string is exactly the set of states the
NFA could be in after reading theq1same string. 0 0, 1
0
The proof of the strengthened statement is by induction on |w|.
Nikita Borisov (UIUC) CS/ECE 374 28 Fall 2018 28 / 49
Base Case If |w| = q00 then w = . Now q3
1
{q0 , q1 } {q2 , q3 }
1
det(N ) (s , q2) = s = N (s, ) by the defn. of det(N ) and defn. of s

7
Figure 6: NFA N Figure 7: DFA det(N ) with only relevant states
Solution:
0 ⇤
(X , a) = [q2X (q, a)

2
8
e), one can include only the reachable states. To do this,

tart by drawing the initial state of the DFA.


tal construction
While there are states with missing transitions, draw the missing transitions creating any new states
hat maybe needed.
CS/ECE
tep 2 is repeated until transitions from 374hasBbeen drawn.
every state Lab 3½ — Solutions Fall 2019
0 appropriately.
tates reachable from s = ✏reach(s) the start state of M
igure out which states are final, and mark them
thod of adding only relevant states is shown in Figures 6 and 7.

Nondeterministic Finite Automata


0, 1

0, 1
{q3 } {}
Mahesh Viswanathan
q1 0 0, 1
0
q0
1 Introducing
q
Nondeterminism
3

{q , q }
1
{q , q } 0 1 2 3
1
q2
Consider the machine shown in Figure 1. Like a DFA it has finitely many states and transitions labeled by
Figure 6: NFA N NFA behavior
symbols from an input alphabet (in this case {0, 1}). However, it has important differences when compared
Figure 7: DFA det(N ) with only relevant states
with the DFA model we(c)
have seen.

[q2X (q, a) 0, 1 0, 1

0
q 0 q0 q00 1 qp
8

Solution:
(UIUC) Machine on input
CS/ECE 374 30 string1:wNondeterministic
Figure
Fall q
from state automaton
can
2018 lead N
30 / 49to set of states
(could be empty) 1 0
1 0
• State q has two outgoing transitions labeled 0.
{ qϵ } 0 { qϵ, q0, q00 } 1 { qϵ, qp } 0 { qϵ, q0, q00, qp }
• States q0 , and q00 have missing transitions. q0 has no transition labeled 1, while q001has no transition
labeled 0.
• State q0 has a transition that is labeled not by an input symbol in {0, 1} but by .
This is an example of what is called a nondeterministic finite automaton (NFA). Intuitvely, such a machine
could have many Work on this
possible later:
computations on a given input. For example, on an input of the form u001v, it
is possible for the machine to reach the accepting state qp by transitioning from q to q0 after reading u.
Nikita Borisov (UIUC) CS/ECE 374 5 Fall 2018 5 / 37
Similarly, it is possible
4. Use for the
the machine to reach
incremental qp also
subset on the inputtou01v
construction — forthe
convert this
NFAsto happen, the machine
from part 1 to DFAs
stays in q as it reads u, transitions to q0 on reading 0 after u, transitions to q00 without reading an input
symbol by following the transition labeled , goes to qp on reading 1, and stays in qp while reading v. On the
other hand, the machine also have other possible computations on both u001v and u01v — it may stay in q
and never transition out of it; or it may transition to q00 on reading u0 by following the -transition from q0
and die attempting to take a transition labeled 0 (that doesn’t exist) out of q00 . The fact that the machines
behavior is not determined by the input string, is the reason these machines are called nondeterministic.

1.1 Nondeterministic Finite Automata (NFA)


NFAs differ from DFAs in that (a) on an input symbol a, a given state may of 0, 1, or more than 1 transition
labeled a, and (b) they can take transitions without reading any symbol from the input; these are the
-transitions 1 . These features are captured in the following formal definition of an NFA.
Definition 1. A nondeterministic finite automaton (NFA) is a M = (Q, , , s, A) where
• Q is a finite set whose elements are called states,
1 Beware: -transitions are not transitions taken on the symbol “ ”. is not a symbol! They are transitions that are taken
without reading any symbol from the input.

You might also like