Martin

John C. Martin · Fourth edition

The Nondeterminism in an NFA Can Be Eliminated

We have observed nondeterminism in two slightly different forms in our discussion of NFAs. It is most apparent if there is a state qq and an alphabet symbol σ\sigma such that several different transitions are possible in state qq on input σ\sigma. A choice of moves can also occur as a result of Λ\Lambda-transitions, because there may be states from which the NFA can make either a transition on an input symbol or one on no input.

We will see in this section that both types of nondeterminism can be eliminated. The idea in the second case is to introduce new transitions so that we no longer need Λ\Lambda-transitions: In every case where there is no σ\sigma-transition from pp to qq but the NFA can go from pp to qq by using one or more Λ\Lambda’s as well as σ\sigma, we will introduce the σ\sigma-transition. The resulting NFA may have even more nondeterminism of the first type than before, but it will be able to accept the same strings without using Λ\Lambda-transitions.

The way we eliminate nondeterminism from an NFA having no Λ\Lambda-transitions is simply to define it away, by finding an appropriate definition of state. We have used this technique twice before, in Section 2.2 when we considered states that were ordered pairs, and in Section 2.5 when we defined a state to be a set of strings. Here a similar approach is already suggested by the way we define the transition function of an NFA, whose value is a set of states. If we say that for an element pp of a set S⊆QS\subseteq Q, the transition on input σ\sigma can possibly go to several states, it sounds like nondeterminism; if we say that starting with an element of the set SS, the set of states to which we can go on input σ\sigma is

⋃{δ(p,σ)∣p∈S}\bigcup\{\delta(p,\sigma)\mid p\in S\}

and if both SS and this set qualify as states in our new definition, then it sounds as though we have eliminated the nondeterminism. The only question then is whether the FA we obtain accepts the same strings as the NFA we started with.

Theorem 3.17

For every language L⊆Σ∗L\subseteq\Sigma^* accepted by an NFA M=(Q,Σ,q0,A,δ)M=(Q,\Sigma,q_0,A,\delta), there is an NFA M1M_1 with no Λ\Lambda-transitions that also accepts LL.

ProofTheorem 3.17

As we have already mentioned, we may need to add transitions in order to guarantee that the same strings will be accepted even when the Λ\Lambda-transitions are eliminated. In addition, if q0∉Aq_0\notin A but Λ∈L\Lambda\in L, we will also make q0q_0 an accepting state of M1M_1 in order to guarantee that M1M_1 accepts Λ\Lambda. We define

M1=(Q,Σ,q0,A1,δ1)M_1=(Q,\Sigma,q_0,A_1,\delta_1)

where for every q∈Qq\in Q, δ1(q,Λ)=∅\delta_1(q,\Lambda)=\varnothing, and for every q∈Qq\in Q and every σ∈Σ\sigma\in\Sigma,

δ1(q,σ)=δ∗(q,σ)\delta_1(q,\sigma)=\delta^*(q,\sigma)

Finally, we define

A1={A∪{q0}if Λ∈LAif notA_1=\begin{cases}A\cup\{q_0\}&\text{if }\Lambda\in L\\A&\text{if not}\end{cases}

For every state qq and every x∈Σ∗x\in\Sigma^*, the way we have defined the extended transition function δ∗\delta^* for the NFA MM tells us that δ∗(q,x)\delta^*(q,x) is the set of states MM can reach by using the symbols of xx together with Λ\Lambda-transitions. The point of our definition of δ1\delta_1 is that we want δ1∗(q,x)\delta_1^*(q,x) to be the same set, even though M1M_1 has no Λ\Lambda-transitions. This may not be true for x=Λx=\Lambda, because δ∗(q,Λ)=Λ({q})\delta^*(q,\Lambda)=\Lambda(\{q\}) and δ1(q,Λ)={q}\delta_1(q,\Lambda)=\{q\}; this is the reason for the definition of A1A_1 above. We sketch the proof that for every qq and every xx with ∣x∣≥1|x|\ge1,

δ1∗(q,x)=δ∗(q,x)\delta_1^*(q,x)=\delta^*(q,x)
Transcription note

In the preceding paragraph, the printed δ1(q,Λ)={q}\delta_1(q,\Lambda)=\{q\} uses the extended transition function: δ1∗(q,Λ)={q}\delta_1^*(q,\Lambda)=\{q\}. The ordinary transition value δ1(q,Λ)\delta_1(q,\Lambda) was defined to be ∅\varnothing.

The proof is by structural induction on xx. If x=a∈Σx=a\in\Sigma, then by definition of δ1\delta_1, δ1(q,x)=δ∗(q,x)\delta_1(q,x)=\delta^*(q,x), and because M1M_1 has no Λ\Lambda-transitions, δ1(q,x)=δ1∗(q,x)\delta_1(q,x)=\delta_1^*(q,x) (see Exercise 3.24).

Suppose that for some yy with ∣y∣≥1|y|\ge1, δ1∗(q,y)=δ∗(q,y)\delta_1^*(q,y)=\delta^*(q,y) for every state qq, and let σ\sigma be an arbitrary element of Σ\Sigma.

δ1∗(q,yσ)=⋃{δ1(p,σ)∣p∈δ1∗(q,y)}=⋃{δ1(p,σ)∣p∈δ∗(q,y)}(by the induction hypothesis)=⋃{δ∗(p,σ)∣p∈δ∗(q,y)}(by definition of δ1)\begin{aligned}\delta_1^*(q,y\sigma)&=\bigcup\{\delta_1(p,\sigma)\mid p\in\delta_1^*(q,y)\}\\&=\bigcup\{\delta_1(p,\sigma)\mid p\in\delta^*(q,y)\}\quad\text{(by the induction hypothesis)}\\&=\bigcup\{\delta^*(p,\sigma)\mid p\in\delta^*(q,y)\}\quad\text{(by definition of }\delta_1\text{)}\end{aligned}

The last step in the induction proof is to check that this last expression is indeed δ∗(q,yσ)\delta^*(q,y\sigma). This is a special case of the general formula

δ∗(q,yz)=⋃{δ∗(p,z)∣p∈δ∗(q,y)}\delta^*(q,yz)=\bigcup\{\delta^*(p,z)\mid p\in\delta^*(q,y)\}

See Exercise 3.30 for the details.

Now we can verify that L(M1)=L(M)=LL(M_1)=L(M)=L. If the string Λ\Lambda is accepted by MM, then it is accepted by M1M_1, because in this case q0∈A1q_0\in A_1 by definition. If Λ∉L(M)\Lambda\notin L(M), then A=A1A=A_1; therefore, q0∉A1q_0\notin A_1, and Λ∉L(M1)\Lambda\notin L(M_1).

Suppose that ∣x∣≥1|x|\ge1. If x∈L(M)x\in L(M), then δ∗(q0,x)\delta^*(q_0,x) contains an element of AA; therefore, since δ∗(q0,x)=δ1∗(q0,x)\delta^*(q_0,x)=\delta_1^*(q_0,x) and A⊆A1A\subseteq A_1, x∈L(M1)x\in L(M_1).

Now suppose ∣x∣≥1|x|\ge1 and x∈L(M1)x\in L(M_1). Then δ1∗(q0,x)\delta_1^*(q_0,x) contains an element of A1A_1. The state q0q_0 is in A1A_1 only if Λ∈L\Lambda\in L; therefore, if δ1∗(q0,x)\delta_1^*(q_0,x) (which is the same as δ∗(q0,x)\delta^*(q_0,x)) contains q0q_0, it also contains every element of AA in Λ({q0})\Lambda(\{q_0\}). In any case, if x∈L(M1)x\in L(M_1), then δ1∗(q0,x)\delta_1^*(q_0,x) must contain an element of AA, which implies that x∈L(M)x\in L(M).

Theorem 3.18

For every language L⊆Σ∗L\subseteq\Sigma^* accepted by an NFA M=(Q,Σ,q0,A,δ)M=(Q,\Sigma,q_0,A,\delta), there is an FA M1=(Q1,Σ,q1,A1,δ1)M_1=(Q_1,\Sigma,q_1,A_1,\delta_1) that also accepts LL.

ProofTheorem 3.18

Because of Theorem 3.17, it is sufficient to prove the theorem in the case when MM has no Λ\Lambda-transitions. The formulas defining δ∗\delta^* are simplified accordingly: δ∗(q,Λ)={q}\delta^*(q,\Lambda)=\{q\} and δ∗(q,xσ)=⋃{δ(p,σ)∣p∈δ∗(q,x)}\delta^*(q,x\sigma)=\bigcup\{\delta(p,\sigma)\mid p\in\delta^*(q,x)\}.

The finite automaton M1M_1 can be defined as follows, using the subset construction: The states of M1M_1 are sets of states of MM, or

Q1=2QQ_1=2^Q

The initial state q1q_1 of Q1Q_1 is {q0}\{q_0\}. For every q∈Q1q\in Q_1 and every σ∈Σ\sigma\in\Sigma,

δ1(q,σ)=⋃{δ(p,σ)∣p∈q}\delta_1(q,\sigma)=\bigcup\{\delta(p,\sigma)\mid p\in q\}

and the accepting states of M1M_1 are defined by the formula

A1={q∈Q1∣q∩A≠∅}A_1=\{q\in Q_1\mid q\cap A\ne\varnothing\}

The last definition is the correct one, because a string xx should be accepted by M1M_1 if, when the NFA MM processes xx, there is at least one state it might end up in that is an element of AA.

There is no doubt that M1M_1 is an ordinary finite automaton. The expression δ1∗(q1,x)\delta_1^*(q_1,x), however, is a set of states of MM—not because M1M_1 is nondeterministic, but because we have defined states of M1M_1 to be sets of states of MM. The fact that the two devices accept the same language follows from the fact that for every x∈Σ∗x\in\Sigma^*,

δ1∗(q1,x)=δ∗(q0,x)\delta_1^*(q_1,x)=\delta^*(q_0,x)

and we now prove this formula using structural induction on xx. We must keep in mind during the proof that δ1∗\delta_1^* and δ∗\delta^* are defined in different ways, because M1M_1 is an FA and MM is an NFA.

If x=Λx=\Lambda, then

δ1∗(q1,x)=δ1∗(q1,Λ)=q1(by the definition of δ1∗)={q0}(by the definition of q1)=δ∗(q0,Λ)(by the definition of δ∗)=δ∗(q0,x)\begin{aligned}\delta_1^*(q_1,x)&=\delta_1^*(q_1,\Lambda)\\&=q_1&&\text{(by the definition of }\delta_1^*\text{)}\\&=\{q_0\}&&\text{(by the definition of }q_1\text{)}\\&=\delta^*(q_0,\Lambda)&&\text{(by the definition of }\delta^*\text{)}\\&=\delta^*(q_0,x)\end{aligned}

The induction hypothesis is that xx is a string for which δ1∗(q1,x)=δ∗(q0,x)\delta_1^*(q_1,x)=\delta^*(q_0,x), and we must show that for every σ∈Σ\sigma\in\Sigma, δ1∗(q1,xσ)=δ∗(q0,xσ)\delta_1^*(q_1,x\sigma)=\delta^*(q_0,x\sigma).

δ1∗(q1,xσ)=δ1(δ1∗(q1,x),σ)(by the definition of δ1∗)=δ1(δ∗(q0,x),σ)(by the induction hypothesis)=⋃{δ(p,σ)∣p∈δ∗(q0,x)}(by the definition of δ1)=δ∗(q0,xσ)(by the definition of δ∗)\begin{aligned}\delta_1^*(q_1,x\sigma)&=\delta_1(\delta_1^*(q_1,x),\sigma)&&\text{(by the definition of }\delta_1^*\text{)}\\&=\delta_1(\delta^*(q_0,x),\sigma)&&\text{(by the induction hypothesis)}\\&=\bigcup\{\delta(p,\sigma)\mid p\in\delta^*(q_0,x)\}&&\text{(by the definition of }\delta_1\text{)}\\&=\delta^*(q_0,x\sigma)&&\text{(by the definition of }\delta^*\text{)}\end{aligned}

A string xx is accepted by M1M_1 precisely if δ1∗(q1,x)∈A1\delta_1^*(q_1,x)\in A_1. We know now that this is true if and only if δ∗(q0,x)∈A1\delta^*(q_0,x)\in A_1; and according to the definition of A1A_1, this is true if and only if δ∗(q0,x)∩A≠∅\delta^*(q_0,x)\cap A\ne\varnothing. Therefore, xx is accepted by M1M_1 if and only if xx is accepted by MM.

We present three examples: one that illustrates the construction in Theorem 3.17, one that illustrates the subset construction in Theorem 3.18, and one in which we use both to convert an NFA with Λ\Lambda-transitions to an ordinary FA.

Example 3.19Eliminating Λ\Lambda-Transitions from an NFA

Figure 3.20a shows the transition diagram for an NFA MM with Λ\Lambda-transitions; it is not hard to see that it accepts the language corresponding to the regular expression (a∗ab(ba)∗)∗(a^*ab(ba)^*)^*. We show in tabular form the values of the transition function δ\delta, as well as the values δ∗(q,a)\delta^*(q,a) and δ∗(q,b)\delta^*(q,b) that will give us the transition function δ1\delta_1 in the resulting NFA M1M_1.

qqδ(q,a)\delta(q,a)δ(q,b)\delta(q,b)δ(q,Λ)\delta(q,\Lambda)δ∗(q,a)\delta^*(q,a)δ∗(q,b)\delta^*(q,b)
1∅\varnothing∅\varnothing{2}\{2\}{2,3}\{2,3\}∅\varnothing
2{2,3}\{2,3\}∅\varnothing∅\varnothing{2,3}\{2,3\}∅\varnothing
3∅\varnothing{4}\{4\}∅\varnothing∅\varnothing{1,2,4}\{1,2,4\}
4∅\varnothing{5}\{5\}{1}\{1\}{2,3}\{2,3\}{5}\{5\}
5{4}\{4\}∅\varnothing∅\varnothing{1,2,4}\{1,2,4\}∅\varnothing

For example, the value δ∗(5,a)\delta^*(5,a) is the set {1,2,4}\{1,2,4\}, because δ(5,a)={4}\delta(5,a)=\{4\} and there are Λ\Lambda-transitions from 4 to 1 and from 1 to 2.

Figure 3.20b shows the NFA M1M_1, whose transition function has the values in the last two columns of the table. In this example, the initial state of MM is already an accepting state, and so drawing the new transitions and eliminating the Λ\Lambda-transitions are the only steps required to obtain M1M_1.

Figure 3.20

Eliminating Λ\Lambda-transitions from an NFA.

Example 3.21Using the Subset Construction to Eliminate Nondeterminism

We consider the NFA M=(Q,{a,b},0,A,δ)M=(Q,\{a,b\},0,A,\delta) in Example 3.6, shown in Figure 3.7. Instead of labeling states as qiq_i, here we will use only the subscript ii. We will describe the FA M1=(2Q,{a,b},{0},A1,δ1)M_1=(2^Q,\{a,b\},\{0\},A_1,\delta_1) obtained from the construction in the proof of Theorem 3.18. Because a set with nn elements has 2n2^n subsets, using this construction might require an exponential increase in the number of states. As this example will illustrate, we can often get by with fewer by considering only the states of M1M_1 (subsets of QQ) that are reachable from {0}\{0\}, the initial state of M1M_1.

It is helpful, and in fact recommended, to use a transition table for δ\delta in order to obtain the values of δ1\delta_1. The table is shown below.

qqδ(q,a)\delta(q,a)δ(q,b)\delta(q,b)
0{1,2}\{1,2\}{4}\{4\}
1{0}\{0\}∅\varnothing
2{3}\{3\}∅\varnothing
3∅\varnothing{0}\{0\}
4∅\varnothing∅\varnothing

The transition diagram for M1M_1 is shown in Figure 3.22. For example, δ1({1,2},a)=δ(1,a)∪δ(2,a)={0,3}\delta_1(\{1,2\},a)=\delta(1,a)\cup\delta(2,a)=\{0,3\}. If you compare Figure 3.22 to Figure 2.23c, you will see that they are the same except for the way the states are labeled. The subset construction doesn’t always produce the FA with the fewest possible states, but in this example it does.

Figure 3.22

Applying the subset construction to the NFA in Example 3.21.

Example 3.23Converting an NFA with Λ\Lambda-Transitions to an FA

For the NFA pictured in Figure 3.24a, we show the transition function in tabular form below, as well as the transition function for the resulting NFA without Λ\Lambda-transitions. It is pictured in Figure 3.24b.

qqδ(q,a)\delta(q,a)δ(q,b)\delta(q,b)δ(q,Λ)\delta(q,\Lambda)δ∗(q,a)\delta^*(q,a)δ∗(q,b)\delta^*(q,b)
1{1}\{1\}∅\varnothing{2,4}\{2,4\}{1,2,3,4,5}\{1,2,3,4,5\}{4,5}\{4,5\}
2{3}\{3\}{5}\{5\}∅\varnothing{3}\{3\}{5}\{5\}
3∅\varnothing{2}\{2\}∅\varnothing∅\varnothing{2}\{2\}
4{5}\{5\}{4}\{4\}∅\varnothing{5}\{5\}{4}\{4\}
5∅\varnothing∅\varnothing∅\varnothing∅\varnothing∅\varnothing
Figure 3.24

Converting an NFA to an FA.

The subset construction gives us a slightly greater variety of subsets this time, but still considerably fewer than the total number of subsets of QQ. The final FA is shown in Figure 3.24c.