Assignment Toc 2
Assignment Toc 2
Assignment TOC 2
Theory of Computation
1. Assume that the alphabet is {0,1}. Give the state diagram of a DFA that
recognizes the language {w | w ends with 00}.
Answer: The key idea is to design three states q0,q1,q2, where q0 specifies the input
string does not end with 0, q1 specifies the input string ends with exactly one 0, and
q2 specifies the input string ends with at least two 0s.
2. Assume that the alphabet is {0,1}. Give the state diagram of a DFA that
recognizes the language {w | w contains an equal number of occurrences of the
substrings 01 and 01}.
Answer: The key observation is that the number of occurrences of the substrings
01 and 01 can differ by at most 1. In the figure below, the states q2 and q4 refer to
the cases when the difference is exactly 1 (q2 when 01 has one more occurrences,
q4 when 10 has one more occurrences).
3. Prove that the language {wp | p is prime} is not regular. (You may assume that
the number of primes is infinite.)
Answer: Suppose on the contrary that this language L is regular. Let p be its
pumping length and t be a prime greater than p. (Note that t exists since the number
of primes is infinite.) Then, wt ∈ L, and by the pumping lemma, we can write wt as
wt = xyz for some x,y,z, such that |y| > 0, |xy| ≤ p, and for each i ≥ 0, xyiz ∈ L.
By setting i = t + 1, the pumping lemma implies that xyt+1z is in L. On the other
hand, xyt+1z = wt+t|y|, which implies that xyt+1z /∈ L, as t + t|y| = t(1 + |y|) is not a
prime.
Thus, a contradiction occurs, so that L is not regular.
Answer:
(a) Suppose on the contrary that F is regular. Let L = {x | x begins with one a}.
Obviously, L is regular. Recall that in the tutorial, we have proved that the
intersection of two regular languages is regular, so the language L0 = F ∩ L is
regular. Let p be the pumping length of L0. Note that L0 = {abncn | n ≥ 0}, so
that abpcp is in L0. By pumping lemma, abpcp can be written as xyz such that |y|
> 0, |xy| ≤ p, and for each i ≥ 0, xyiz ∈ L0. However, (i) if y includes a, the
string xyyz has at least two a’s; (ii) else, if y includes both b and c, the string
xyyz has at least two substrings bc; (iii) else, y includes only b or only c, and
so the number of b and the number of c in xyyz will not be equal. In all the
above three cases, xyyz cannot be in L0, so that a contradiction occurs (why?).
Thus, F is not regular.
(b) Let p0 be the pumping length. Let and let s be a string in F with
|s| > p. We divide the discussion into two cases such that Case 1 corresponds
to those string s that has exactly two ‘a’s (i.e., when i = 2), and Case 2
corresponds to the other kinds of s (i.e., when i = 2)6 . For Case 1, we can
divide s into xyz, such that x = ε, y = aa, and z is the remaining string. For Case
2, we can divide s into xyz, such that x = ε, y is the first character, and z is the
remainder string. We can easily verify that in both cases, |y| > 0, |xy| ≤ p, and
for all i ≥ 0, xyiz ∈ F.
(c) All regular language satisfies the pumping lemma, but the converse is not true.
That is, if a language satisfies the pumping lemma, the language may not be
regular.
5. For languages A and B, let the perfect shuffle of A and B be the language
by construction. However, the key difference is that D may now switch from
running DA and running DB after each character is read. To allow this flexibility
and simplify the construction, we design an NFA N = (Q,Σ,δ,q,F) that recognizes
the shuffle of A and B instead of directly designing a DFA.
At any time, N needs to keep track of the current states of DA and DB. Then, when
a character is read, N may make a move in DA or DB accordingly. After the whole
string is processed, if both DFAs are in the accept states, the input string is
accepted; otherwise, the input string is rejected. In addition, N should also accept
the empty string.
Formally, the NFA N can be defined as follows:
(a) Q = (QA × QB) ∪ {q0}, where QA × QB keeps track of all possible current states
of DA and DB, and q0 denotes the state when nothing is read.
(b) q = q0.
(c) F = (FA ×FB)∪{q0}, which states that N accepts the string if both DA and DB
are in accept states, or N accepts the empty string.
(d) δ is as follows:
i. δ(q0,ε) = (qA,qB), which states that at the start state q0, N can make DA in qA
and DB in qB without reading anything.
ii. (δA(x,a),y) ∈ δ((x,y),a), which states that if current state of DA is x, the
current state of DB is y, then when a is read as the next character, we can
change the current state of A to δA(x,a), while the current state of B is not
changed. iii. Similarly, (x,δB(y,a)) ∈ δ((x,y),a).
iii.
(a) Show that if L is recognized by a DFA with k states, L has index at most k.
(a) Let M be the DFA with k states recognizing L. Suppose on the contrary that L
has index greater than k. That means, some set X with at least k + 1 strings is
pairwise distinguishable by L. By pigeonhole’s principle, we can find two
distinct strings x and y from X, such that the state of M after reading x as input
is the same as the state of M after reading y as input. Therefore, both xz and yz
are in L or neither are in L. This implies x and y are not distinguishable by L.
Contradiction occurs, so that L has index at most k.
(b) Let X = {x1,x2,...,xk} be pairwise distinguishable by L. We construct DFA M =
(Q,Σ,δ,q0,F) with k states recognizing L as follows. Let Q = {q1,q2,...,qk}, and
define δ(qi,a) to be qj, if xia and xj are not distinguishable. Let F = {qi | xi ∈ L}.
Let the start state q0 be the (unique) state such that xi and and the empty string
ε are not distinguishable by L.
We can show that if a string s and xj are not distinguishable by L, the state of
M after reading s as input will be qj (how to show?). Then, by the definition of
F, M accepts s if and only if s is in L (why?). Thus, M recognizes L.
(c) Suppose that L is regular and let k be the number of states in a DFA
recognizing L. Then from part (a), L has index at most k. On the other hand,
if L has index k, then from part (b) we can construct a DFA with k states
recognizing L. Thus, L is regular if and only if it has finite index.
To see why the index k is the size of the smallest DFA recognizing it, suppose
on the contrary that it is not true. Then, from part (a) we would conclude that
L has index fewer than k, which contradicts with the fact that L has index equal
to k.
far, and the possible states which can reach some accept state of D in i steps. Then,
a string is accepted by N when the current state is in one of these possible states.
Formally, we let N = (Q,Σ,δ,q,F) such that
(a) Q = (QA × QA) ∪ {q0}, where QA × QA keeps track of the current state of D, and
the state that can reach an accept state of D in i steps, where i is the length of
the input string processed so far. In addition, we create a state q0, which
denotes the state when nothing is read.
(b) q = q0.
(c) F = {(x,x) | x ∈ QA}, which states that a string is accepted when the current
state of D is at x, and x is i steps from some accept state of D, where i is the
length of the input string processed so far.
(d) δ is as follows:
i. (qA,x) ∈ δ(q0,ε) for x ∈ FA, which states that without reading anything, we
make D to start at qA, and keep track that x ∈ F is 0 steps from some accept
state of D.
ii. (δA(x,a),z) ∈ δ((x,y),a) for any z such that there exists some c ∈ Σ with
δA(z,c) = y. This states that when D advances one step from state x to
δA(x,a), we update the state y to some state z which is one more step further
from the accept state of D.