Thompson Algorithm
Thompson Algorithm
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
ϵ ϵ 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
ϵ
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,
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,
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.