A1 Solutions
A1 Solutions
A1 Solutions
The submission portal on GS, and an Ed post explaining it, will open in Week 4. Here is some
early information.
The submission formats for regular expressions and automata are detailed here:
Regex submission format
DFA/NFA submission formats
For Problems 1 through 4 you will submit files. The file names must be:
problem 1 1.txt
problem 1 2.txt
problem 2 1.txt
problem 2 2.txt
problem 3 1 re.txt
problem 3 1 dfa.txt
problem 3 2 re.txt
problem 3 2 dfa.txt
problem 3 3 re.txt
problem 3 3 dfa.txt
problem 4 1 nfa.txt
problem 4 1 x.txt
problem 4 2 nfa.txt
The submission process for Problem 5 will be discussed in the forthcoming Ed post.
1. ( a∗ b)∗ | a∗
2. (Σ∗ aaΣ∗ )|(Σ∗ bbΣ∗ )|( ab)∗ |(ba)∗ | a|b
The first line of each answer file should contain a comma separated sequence of
five strings that are in the language, and the second line should contain a comma
separated sequence of five strings that are not in the language. For example, if
the regular expression was b∗ , an example of a correct text file would be:
1
comp2022/2922 Assignment 1 (90 marks) – Regular Languages s2 2023
2.
Sigma = a b
Q = q0 q1 q2 q3
start = q0
F = q2
q0 a q1
q0 b q2
q1 a q0
q1 b q0
q2 a q3
q2 b q3
q3 a q3
q3 b q3
Sigma = a b
Q = q0 q1 q2
start = q0
F = q1 q2
q0 a q1
q0 b q1
q0 a q2
q0 b q2
q1 a q2
q1 b q2
q2 a q2
q2 b q0
2
comp2022/2922 Assignment 1 (90 marks) – Regular Languages s2 2023
2.
1. The set of strings that contain abbab, in that order, but not necessarily con-
secutively.
Said differently, the set of strings such that one can delete some amount of
letters, possibly zero, to obtain abbab.
For example, aaababaababba is in the language, while bbbababbbaa is not.
2. The set of strings of length 4 that contain exactly two bs.
For example, abba is in the language, while abbb and abb are not.
3. The set of strings that contain exactly three as and an even number of bs.
For example, abababb is in the language, while ababab is not.
Solution 3. Here are DFAs for each language (where rejecting sinks are not
drawn):
1. b a a b a a, b
q0 a q1 b q2 b q3 a q4 b q5
To see that this DFA M is correct we reason as follows. Let L be the language
described in the question and M the drawn automaton. We must show that
L = L( M). To do this we will reason that L( M ) ⊆ L and then reason that
L ⊆ L( M ). To see that L( M) ⊆ L, note that the only way to get to the
final state is to read abbab in that order, possibly with some other letters
inbetween. To see that L ⊆ L( M), consider a string u ∈ L. Intuitively, the
DFA finds the first subsequence of the form abbab in u. To see what ’first’
3
comp2022/2922 Assignment 1 (90 marks) – Regular Languages s2 2023
means, note that every subsequence of the form abbab is identified by the
sequence of positions of its letters in u. For instance if u = baabbbaba then
baabbbaba(1, 3, 4, 6, 7)
baabbbaba(1, 3, 5, 6, 7)
baabbbaba(1, 4, 5, 6, 7)
baabbbaba(2, 3, 4, 6, 7)
baabbbaba(2, 3, 5, 6, 7)
baabbbaba(2, 4, 5, 6, 7)
b b b
b b b
3. The idea is that the x-axis records the number of a’s, and the y-axis the num-
ber of b’s modulo 2. q a q a q a q
0 1 2 3
b b b b b b b b
4
comp2022/2922 Assignment 1 (90 marks) – Regular Languages s2 2023
( EaEaEaE)|(OaOaEaE)|(OaEaOaE)|(OaEaEaO)|
( EaOaOaE)|( EaOaEaO)|( EaEaOaO)|(OaOaOaO)
5
comp2022/2922 Assignment 1 (90 marks) – Regular Languages s2 2023
a a, b
a, b
q0 q2
b
a, b
a
q1
There are two pairs (q, x ) where |δ(q, x )| > 1, i.e., (q0 , a) and (q2 , b). Hence there
are 6 possible clippings of N, corresponding to fixing one of the 3 choices for
(q0 , a) and one of the 2 choices for (q2 , b).
1. Consider the following claim: “For every epsilon-free NFA N and string x,
the NFA N accepts x if and only if some clipping of N accepts x.” This claim
is false. Show that it is false by providing a counterexample. (10 marks)
(Aside: It turns out that deciding whether N accepts x is in P, while deciding
whether some clipping of N accepts x is NP-complete, which you will learn
about later in the course. Hence, if the claim were true, it would imply that
P = NP.)
2. Consider the following claim: “For every epsilon-free NFA N, the NFA N is
universal if and only if some clipping of N is universal.” This claim is also
false. Show that it is false by providing a counterexample. (10 marks)
Solution 4.
a a
1. The following automaton accepts x = aa (since q0 − → q0 − → q1 is an accept-
ing run), but no clipping of it accepts x. In particular, in any clipping, either
a a a
q0 −
→ q0 or q0 −→ q1 must be removed. If q0 − → q0 is removed, then the only
a
run on aa ends at q0 and so x is rejected. If q0 −→ q1 is removed, then there
is no run on aa and so x is rejected.
6
comp2022/2922 Assignment 1 (90 marks) – Regular Languages s2 2023
q0 a q1
a, b a, b
a, b
q2 q3
7
comp2022/2922 Assignment 1 (90 marks) – Regular Languages s2 2023
and if M′ accepts XXCXT then M must accept at least one of the following
strings: AACAT, CCCCT, GGCGT, TTCTT.
For example, suppose M is the following automaton:
A, C, G, T
q0 A q1 T q2
A T, X
q0 q1 q2
X T
q3
A, C, G, T, X
Solution 5. We use the following constructions. Note that others exist, e.g. we
could use the construction for (2) in (1).
1. For the NFA version, the idea is to form an NFA as the union of four NFAs
that decide which of A, C, G, T the X’s should be replaced with. The union
is done with the union construction for NFAs, i.e., by adding a new initial
state and epsilon transitions to the initial states of each of the four NFAs.
Here are the details. For every state qi in M, create four states in M′ :
Aqi , Cqi , Gqi , Tqi each indicating that the X in the string should be sub-
stituted with A, C, G, T respectively. Also create a new initial state q0′ for
M′ .
For each transition q j ∈ δ(qi , Y ) in M (here Y ∈ { A, C, G, T, ϵ}), we add
a transition for each of A, C, G, T variants, i.e. Zq j ∈ δ′ ( Zqi , Y ) for each
8
comp2022/2922 Assignment 1 (90 marks) – Regular Languages s2 2023