CSC236 A6
CSC236 A6
Assignment 6
Unmarked Questions
Here are a few simple warm-up problems. Make sure you are able to do them before proceeding to
the marked questions.
For each of the following languages, state whether or not it is regular and prove your answer. If
the language is regular, draw a finite automaton (DFA or NFA) which accepts the language using
no more than 10 states and proof that it accepts the language (during an exam, you will not have
to do this). Otherwise, prove that the language is not regular.
a. L1 = {w ∈ {0, 1}∗ : w contains both 01 and 00 as substrings}. For example, the strings
001, 010100, 000111 ∈ L1 , while the strings 0, 1111, 010101 ∈
/ L1 .
Solution. L1 is regular. We will draw a DFA which accepts L1 . It is shown in Figure 1.
1 1
0
q0 q1 q2
0 1 1 0
q3 q4 q5
0 1
0 0, 1
Proof. To prove that M indeed accepts L, we will come up with and prove a state invariant
for each of the six states. The predicate on a string w ∈ {0, 1}∗ will be
q0 w = 1k for k ≥ 0
q1 w = 1k 01(1ℓ + (01)m )n for k, ℓ, m, n ≥ 0
q w = 1k 01(1ℓ + (01)m )n 0 for k, ℓ, m, n ≥ 0
2
P (w) := δ ∗ (q0 , w) = (1)
q3 w = 1k 0 for k ≥ 0
q4 w = 1k 00(0)ℓ for k, ℓ ≥ 0
q
5 w constains both the string 01 and 00 as substrings
1
CSC236H1Y 20245 Last updated: August 3, 2024
We note that these six states are disjoint, i.e., no string can belong to two different states,
and exhaustive, i.e., every string will end up at some state.
We prove P (w) by structural induction with our standard recursive definition of Σ∗ . In the
base case δ ∗ (q0 , ϵ) = q0 and indeed ϵ can be written as 1k with k = 0.
Suppose for some string w ∈ Σ∗ , P (w) is true. We show that P (w0) and P (w1) are also true.
For w0, we need to consider each of six possible cases for δ ∗ (q0 , w0).
δ ∗ (q0 , w) = q0 We know that w = 1k for k ≥ 0 (IH). On a zero, δ, will take the transition to q3 so
δ ∗ (q0 , w0) = q3 . Note that the invariant for q3 is strings of the form 1k 0 for k ≥ 0.
n
δ ∗ (q0 , w) = q1 We know that w = 1k 01 1ℓ + (01)m for k, ℓ, m, n ≥ 0 (IH). On a zero, δ, will take the
transition to q2 so δ ∗ (q0 , w0) = q2 . Note that the invariant for q2 is exactly strings of
the form 1k 01(1ℓ + (01)m )n 0 for k, ℓ, m, n ≥ 0.
n
δ ∗ (q0 , w) = q2 We know that w = 1k 01 1ℓ + (01)m 0 for k, ℓ, m, n ≥ 0 (IH). On a zero, δ, will take
the transition to q5 so δ ∗ (q0 , w0) = q5 . Note that the invariant for q5 is exactly strings
containing both 01 and 00 as substrings and by adding a zero we guarantee that w0 now
contains 00 as a substring (it already had a 01 substring).
δ ∗ (q0 , w) = q3 We know that w = 1k 0 for k ≥ 0 (IH). On a zero, δ, will take the transition to q4 so
δ ∗ (q0 , w0) = q4 . Note that the invariant for q4 is exactly strings of the form 1k 00(0)ℓ for
k, ℓ ≥ 0. Here, we could take ℓ = 0.
δ ∗ (q0 , w) = q4 We know that w = 1k 00(0)ℓ for k, ℓ ≥ 0 (IH). On a zero, δ, will take the transition to
q4 so δ ∗ (q0 , w0) = q4 . Note that the invariant for q4 is still satisfied as we now have
1k 00(0)ℓ+1 for k, ℓ ≥ 0.
δ ∗ (q0 , w) = q5 We know that w is a string with both 01 and 00 as substrings (IH). It follows that w0
also satisfies this state invariant as well.
We will also need do the same for w1, but it is a bit tedious so we won’t write it out explicitly.
By Structural induction, P (w) is true for all w ∈ Σ∗ and our DFA indeed accepts L1 .
b. L2 = {0n : n is a perfect square}. For example, the strings ϵ, 0, 0000 are in L2 , but the
strings 00, 000, 00000 are not.
Solution. L2 is not regular, suppose for a contradiction that L2 is regular and let M be
a DFA which accepts L2 . Suppose that the M has pumping length p. Consider string
2
w = 0(p+1) . Since w ∈ L2 , by the Pumping Lemma, we know that w = xyz where |xy| ≤ p,
|y| ≥ 1, and xy 2 z ∈ L2 . We claim that this is a contradiction since xy 2 z ∈ / L2 . Since
w consists of only zeros, y = 0a for some constant a ≤ p. Note that xy 2 z = x02a z. We
know that |x02a z| > (p + 1)2 since the right hand side is the length of w. Note however
|x02a z| = (p + 1)2 + a < (p + 1)2 + 2(p + 1) + 1 = (p + 2)2 . Since there are no perfect squares
greater than (p + 1)2 and less than (p + 2)2 , x02a z ∈
/ L2 .
Marked Questions
Consider the marble rolling toy shown in Figure 2. A marble is dropped at A or B. There are
flippers at positions x1 , x2 , and x3 (indicated by the thick black lines) which fall either to the left
2
CSC236H1Y 20245 Last updated: August 3, 2024
or to the right. Whenever a marble encounters a flipper, it causes the flipper to reverse; the next
marble will take the opposite branch.
A B
x1 x2
x3
C D
Figure 2: Default configuration of the marble rolling toy. Marbles are dropped in one of A or B
and come out at one of C or D.
Model the toy as a DFA M = (Q, Σ, δ, s, F ) with alphabet Σ = {A, B}. Inputs to the DFA are
sequences of As and Bs indicating where the marbles will be dropped. M should accept a string of
As and Bs if the last marble dropped exits the toy through D. Accept the empty string.
An example of the state of the machine after dropping marbles consecutively at A, A, and then B
is shown in Figure 3.
a. Draw your DFA below. You should not need more than sixteen states.
Solution.
A B A B A B
x1 x2 x1 x2 x1 x2
x3 x3 x3
C D C D C D
(a) Drop first marble at A. (b) Drop second marble at A. (c) Drop third marble at B.
Figure 3: Result of dropping three marbles at A, then A, then B. This corresponds to the three
strings A, AA, and AAB. Your DFA should reject A and AA but accept AAB.
3
CSC236H1Y 20245 Last updated: August 3, 2024
b. Formally prove that your DFA is correct. In your inductive case, you only need to consider
the addition of A to the end of a string w such that P (w) is true for your predicate P .
Proof.
4
CSC236H1Y 20245 Last updated: August 3, 2024
Q2. [10 Points] Regex Implies NFA (maximum 2 pages not counting the diagrams for Regex
to NFA solutions)
In the following question we will complete the proof of the equivalence of regular expressions
(Regex), deterministic finite automaton (DFA), and non-deterministic finite automaton (NFA). In
class, we showed that DFAs have the same expressive power as NFAs. In this problem, we will first
turn a DFA M into a regex R such that the language accepted by M , denoted L(M ), is equivalent
to the language represented by R, denoted L(R), i.e., we want to find a regular expression R which
is equivalent to M . Then we will show that a regex R can be transformed into a NFA N such that
L(R) = L(N ), i.e., we want to find an NFA N which is equivalent to R.
DFA→Regex We will walk through the state removal method. The goal is to remove states until there
are only two remaining, the start state and a single accepting state with a single transition
between them. We do this by replacing single character transitions with regular expressions.
(a) Let R be a regex and suppose the DFA M is as shown in Figure 4. Find a regex which
is equivalent to M .
R
q0 q1
Figure 4: DFA M .
Solution.
(b) Suppose instead that M has many more states and let a part of M be shown on the left
of Figure 5. We want to eliminate state qi+1 so that M looks like the right of Figure 5
afterwards. Find a regex to put on the transition qi → qi+2 using R1 , R2 , and R3 so
R2
R1 R3 ?
··· qi qi+1 qi+2 ··· ··· qi qi+2 ···
5
CSC236H1Y 20245 Last updated: August 3, 2024
R1
?
··· qj .. qj+1 ··· ··· qj qj+1 ···
.
Rk
Proof.
Regex→NFA Recall that regexs are defined recursively on the set of characters Σ. For each part of the
recursive definition of regexs, we construct an equivalent NFA. A diagram is enough; you do
not have to prove the correctness of the NFA.
Additional Questions
If you would like more exercises consider trying the following problems from your primary and
supplementary textbooks. We will not be providing solutions to these questions though you are
free to find the solution online and discuss them with your peers.