CMPS 260 Spring 2024
Homework #3: Nondeterministic Finite Automata
Sample Solutions

Part I: NFAs

Each of the first five problems describes a language and asks you to present an NFA —with a specified maximum number of states and/or transitions— that accepts that language. In counting states and transitions, you need not include the dead state or any transitions directed to it. Nor do you need to show that state or those transitions, as it is understood that if, in a transition graph, a state q has no outgoing transition on a symbol a, it is as though there were such a transition and it went to the dead state.

1. Present an NFA having at most five states and eight transitions that accepts the language

L1 = { 0u110v  |  u,v ∈ {0,1}* }

In words, L1 contains precisely those strings over the alphabet {0,1} having 0 as a prefix and 110 as a substring.

Note that a transition labeled by both 0 and 1 counts as two transitions.

Solution:


2. Present an NFA having at most six states and at most nine transitions that accepts the language

L2 = { w ∈ {0,1}*  |  w = x011y for some x,y ∈{0,1}*  ∨  w = x0100 for some x ∈ {0,1}*}

In words, L2 contains precisely those strings over the alphabet {0,1} having either 011 as a substring or 0100 as a suffix.

Note that a transition labeled by both 0 and 1 counts as two transitions.

Solution:


3. Present an NFA having at most five states (only one final state is necessary) that accepts the language

L3 = { wc  |  w ∈ {a,b}*} ∪ { wb  |  w ∈ {a,c}*} ∪ { wa  |  w ∈ {b,c}*}

In words, L3 contains precisely those strings over the alphabet {a,b,c} ending in a symbol that appears exactly once.

Note that using λ-transitions would be helpful.

Solution:


4. Present an NFA having at most eight states (only one final state is necessary) that accepts the language

L4 = { wbabv  |  w,v ∈ {a,b}* ∧ |v| ≥ 4} ∪ { wbbv  |  w,v ∈ {a,b}* ∧ |v| = 2}

In words, L4 contains precisely those strings over {a,b} that end either
(i) four or more symbols after an occurrence of bab, or
(ii) exactly two symbols after an occurrence of bb.

Solution:


5. Present an NFA having four states, and only two transitions labeled a, that accepts the language

L5 = { {aa} ∪ {aba} ∪ {cca} }*

In words, L5 contains precisely those strings over {a,b,c} that can be "factored" into a sequence of zero or more substrings, where each factor is either aa, aba, or cca. An example of such a string (in which vertical bars have been inserted in order to show the boundaries between adjacent factors) is

aba|aa|aa|cca|aa|aba|aba|cca|aba|aa

You will find it helpful to use at least one λ transition.

Solution:

The following DFA also accepts the specified language, and has only four states, but it has three transitions on a and therefore does not meet the requirements of the problem.



6. Construct a λ-free NFA that is equivalent to (i.e., accepts the same language as) the NFA shown below. Present your answer in tabular form (as on the right side of the figure below) rather than as (or in addition to, if you want) a transition graph. Don't forget to identify the initial state and the final states.

An appropriate algorithm for removing λ-transitions is described in the section titled Step 1 in the web page that describes the Subset Construction algorithm (by which to obtain a DFA equivalent to a given NFA).

Transition Graph Table
Init?Final?State δ(_,a)δ(_,b)δ(_,λ)
q0{q0,q1}
q1{q1} {q0} {q2}
q2 {q3} {q3}
q3{q4}
q4 {q1} {q0}

Solution: Because of the large number of transitions, the graph would be a mess and hence is not shown. In the description of the δ function in the table, states were referred to by the subscripts in their names. Thus, for example, {0,3} is to be understood as meaning {q0, q3}.

Init?Final?State δ(_,a)δ(_,b)
q0{0,1,2,3}
q1{0,1,2,3,4} {0,3}
q2{0,4} {3}
q3{0,4}
q4{0,1,2,3} {1,2,3}


7. Present the DFA obtained by applying the Subset Construction algorithm to the NFA shown below. (It should have seven states, including the dead state.)

Present your answer in tabular form (as on the right side of the figure below) rather than as (or in addition to, if you want) a transition graph. Don't forget to identify the initial state and the final states.

Your DFA's state names should make obvious which subset of the NFA's state set each one corresponds to. For example, q{0,2} (or simply {0,2}, if you prefer) could be the name of the state corresponding to the set of states {q0, q2} in the given NFA.

Transition Graph Table
Init?Final?State δ(_,a)δ(_,b)
q0{q1} {q2}
q1 {q0,q1}
q2{q2} {q1}

Solution: In the transition graph, states are referred to by the subscripts in their names. Thus, for example, {1,2} is to be understood as meaning q{1,2}.

Transition Graph Table
Init?Final?State δ(_,a)δ(_,b)
q{0}q{1} q{2}
q{1}q q{0,1}
qq q
q{2}q{2} q{1}
q{0,1}q{1} q{0,1,2}
q{1,2}q{2} q{0,1}
q{0,1,2}q{1,2} q{0,1,2}



Part II: Regular Expressions

8. Present a list containing precisely those strings of length five that are members of L( (b(bc + a)* ). Arrange the strings in alphabetical order.

Solution: b.a.a.a.a, b.a.a.bc, b.a.bc.a, b.bc.a.a, b.bc.bc


9. Present a list containing precisely those strings of length six or less that are members of

L( a(ba + a)*b(a + ab)* ).

Arrange the strings from shortest to longest; within each group of strings having the same length, put them in alphabetical order.

Solution:
Length 2: a.b
Length 3: a.a.b, a.b.a
Length 4: a.a.a.b, a.a.b.a, a.b.a.a, a.b.ab (or a.ba.b)
Length 5: a.a.a.a.b, a.a.a.b.a, a.a.b.a.a, a.a.ba.b, a.b.a.a.a, a.ba.a.b, a.ba.b.a
Length 6: a.a.a.a.a.b, a.a.a.a.b.a, a.a.a.b.a.a, a.a.a.b.ab, a.a.b.a.a.a, a.a.ba.a.b, a.a.ba.b.a (or a.a.b.ab.a), a.b.a.a.a.a, a.b.a.a.ab (or a.ba.a.a.b), a.ba.a.b.a, a.ba.b.a.a, a.ba.ba.b (or a.ba.b.ab or a.b.ab.ab)


10. Present a regular expression describing LR (the reverse of L), where L is the language described by the regular expression

(bba + bc)*abc(ba + a)*

Solution:

(ab + a)*cba(abb + cb)*