Assignment 1 solutions

Transcription

Assignment 1 solutions
CS 360 Introduction to the Theory of Computing
Spring 2015
Assignment 1 solutions
1. [8 points] The following problems each have a short answer, perhaps just a few sentences and
maybe an equation or two. Try to make your answers clear and to the point—and choose the
simplest answer whenever possible.
(a) Suppose that A and B are nonempty sets for which B ⊆ A, and assume that there exists
an onto function f of the form f : N → A. Prove that there exists an onto function of the
form g : N → B. What do you conclude from this fact about any subset of a countable
set?
(b) Prove that there are countably many finite languages over any alphabet Σ.
(c) Prove that there exist non-regular languages A, B ⊆ {0, 1}∗ for which the language AB is
regular.
(d) Suppose that A, B ⊆ {0, 1}∗ are languages for which it holds that A ⊆ B and B is regular.
Is it necessarily the case that A is regular? Give a short argument in support of your
answer.
Solution.
(a) Let b ∈ B be any fixed element in B; such an element exists by the assumption that B is
nonempty. Define g : N → B as follows:
(
f (n) if f (n) ∈ B
g(n) =
b
if f (n) 6∈ B.
It is evident that g(n) ∈ B for every n ∈ N. For any element x ∈ B, one has x ∈ A by the
assumption that B ⊆ A, and therefore x = f (n) for some n ∈ N by the assumption that f is onto.
As it must hold that g(n) = f (n) = x in this situation, it follows that g is onto, as required.
From this fact we conclude that every nonempty subset of a countable set is also countable.
(b) Here are two possible solutions to this problem (but of course you were only expected to
provide one answer):
The first solution uses the fact (discussed in class) that there are countably many regular languages
over any alphabet Σ. Because every finite language is regular, one has that the set of finite languages is a subset of the set of regular languages—so the fact that there are countably many finite
languages over Σ follows.
The second solution argues directly that there are countably many finite languages over an alphabet Σ. One simple way to argue this is to observe that for every n ∈ N, there are only finitely
many languages over Σ that have at most n strings and are such that each string in the language
has length at most n. (Note that we are using n to limit two separate aspects of these languages:
the number of elements and the length of each element. This sort of trick will often be used later
in the course.) If we list all of the languages obtained in this way for increasing values of n, we
obtain a countable sequence of finite languages. As every finite language appears somewhere in
the sequence, we conclude that there are countably many finite languages.
(c) There are many possible solutions to this problem, and here is one. Let C ⊆ {0, 1}∗ be any
nonregular language, such as
C = { 0n 1n : n ∈ N } .
Define
A = {ε} ∪ C
and
B = {ε} ∪ C.
Adding the empty string (or any other single string) to a nonregular language yields another
nonregular language, so A and B are nonregular. It is the case, however, that AB = Σ∗ , which is a
regular language.
(d) The answer is “no.” For example, let B = {0, 1}∗ , which is regular, and let
A = { 0n 1n : n ∈ N } ,
which is nonregular. It holds that A ⊆ B, so this represents a counter-example to the statement
that A is regular.
2. [5 points] Suppose M = ( Q, Σ, δ, q0 , F ) is a DFA, and suppose that r ∈ Q is a state of M that is
different from the start state q0 . Define A ⊆ Σ∗ to be the language consisting of all strings that
are accepted by M and cause M to enter the state r at least once. Give a precise description of
a DFA K that recognizes the language A.
Solution. We may define K as follows:
K = {0, 1} × Q, Σ, µ, (0, q0 ), {(1, q) : q ∈ F } ,
where
µ : {0, 1} × Q × Σ → {0, 1} × Q
is the transition function of K, defined as
(
µ (0, q), σ =
(0, δ(q, σ)) if δ(q, σ) 6= r
(1, δ(q, σ)) if δ(q, σ) = r,
and
µ (1, q), σ = (1, δ(q, σ))
for all choices of q ∈ Q and σ ∈ Σ.
The way K works is that it essentially mimics the behavior of M, but in addition keeps track of a
bit that indicates whether or not r has been visited. The bit starts out as 0, and is flipped to 1 in the
event that the state r is reached. The bit is never flipped back once it turns to 1. The accept states
of K are those of the form (1, q) for q ∈ F, as they indicate that M is in an accepting state and the
state r has been touched. (The problem only asks for the description of K, so a correct answer did
not need to include a discussion like this—it is included to add to the clarity of the solution.)
2
3. [8 points] Let Σ = {0, 1} and let A ⊆ Σ∗ be an arbitrarily chosen regular language.
(a) Prove that the language
B = {uv : u, v ∈ Σ∗ and uσv ∈ A for some choice of σ ∈ Σ}
is regular.
(b) Prove that the language
C = {vu : u, v ∈ Σ∗ and uv ∈ A}
is regular.
Solution.
(a) A natural way to solve this problem is to describe an NFA for B, based on a DFA for A, which
must exist by the assumption that A is regular. This will imply that B is regular, as every language
recognized by an NFA is necessarily regular.
Along these lines, let us suppose that
M = ( Q, Σ, δ, q0 , F )
is a DFA for which it holds that A = L( M). Define an NFA
N = ( P, Σ, η, p0 , G )
as follows. First, we will define
P = {0, 1} × Q,
and we will take the start state of N to be
p0 = (0, q0 ).
The accept states of N will be
G = {(1, q) : q ∈ F }.
It remains to describe the transition function η of N, which will be as follows:
(i) η ((0, q), σ) = {(0, δ(q, σ))} for every q ∈ Q and σ ∈ Σ.
(ii) η ((0, q), ε) = {(1, δ(q, 0)), (1, δ(q, 1))} for every q ∈ Q.
(iii) η ((1, q), σ) = {(1, δ(q, σ))} for every q ∈ Q and σ ∈ Σ.
(iv) η ((1, q), ε) = ∅ for every q ∈ Q.
The idea behind the way that N operates is as follows. N starts in the state (0, q0 ) and simulates
M for some number of steps. This is the effect of the transitions listed as (i) above. At some point,
which is nondeterministically chosen, N follows an ε-transition from a state of the form (0, q) to
either the state (1, δ(q, 0)) or (1, δ(q, 1)). Intuitively speaking, N is reading nothing from its input
while “hypothesizing” that M has read some symbol σ (which is either 0 or 1). This is the effect of
the transitions listed as (ii). Then N simply continues simulating M on the remainder of the input
3
string, which is the effect of the transitions listed as (iii). There are no ε-transitions leading out of
the states of the form (1, q), which is why we have the values for η listed as (iv).
I would not expect you to give a detailed, low-level proof for the claim that L( N ) = B. That
would of course be possible, but if you understand how the NFA N is working, it would be fair to
say that this fact is reasonably clear. Given that B = L( N ), we have that B is regular, as required.
Here is a somewhat different solution that may appeal to some of you. Part of its appeal is that
it illustrates a method that may be useful in other cases. In this case I will also give a somewhat
more detailed proof of correctness (partly because it happens to be a bit easier for this solution).
Again, let
M = ( Q, Σ, δ, q0 , F )
be a DFA for which L( M) = A. For each choice of p, q ∈ Q, define a new DFA
M p,q = ( Q, Σ, δ, p, {q}),
and let A p,q = L( M p,q ). In words, A p,q is the regular language consisting of all strings that cause
M to transition to the state q when started in the state p. For any choice of p, q, and r, we must
surely have A p,r Ar,q ⊆ A p,q . Indeed, A p,r Ar,q represents all of the strings that cause M to transition
from p to q, touching r somewhere along the way.
Now consider the language
[
Aq0 ,p Aδ( p,σ),r .
( p,σ,r )∈ Q×Σ× F
This is a regular language because each A p,q is regular and the regular languages are closed under
finite unions and concatenations. To complete the solution, let us observe that the language above
is none other than B:
[
Aq0 ,p Aδ( p,σ),r .
B=
( p,σ,r )∈ Q×Σ× F
To prove this equality, we do the natural thing, which is to separate it into two separate set
inclusions. First let us prove that
B⊆
[
Aq0 ,p Aδ( p,σ),r .
( p,σ,r )∈ Q×Σ× F
Every string in B takes the form uv, for some choice of u, v ∈ Σ∗ and σ ∈ Σ for which uσv ∈ A.
Let p ∈ Q be the unique state for which u ∈ Aq0 ,p , which we could alternatively describe as
the state of M reached from the start state on input u, and let r ∈ F be the unique state (which is
necessarily an accepting state) for which uσv ∈ Aq0 ,r . As σ causes M to transition from p to δ( p, σ),
it follows that v must cause M to transition from δ( p, σ) to r, i.e., v ∈ Aδ( p,σ),r . It therefore holds
that uv ∈ Aq0 ,p Aδ( p,σ),r , which implies the required inclusion.
Next we will prove that
[
Aq0 ,p Aδ( p,σ),r ⊆ B.
( p,σ,r )∈ Q×Σ× F
The argument is quite similar to the other inclusion just considered. Pick any choice of p ∈ Q,
σ ∈ Σ, and r ∈ F. An element of Aq0 ,p Aδ( p,σ),r must take the form uv for u ∈ Aq0 ,p and v ∈ Aδ( p,σ),r .
One finds that uσv ∈ A p0 ,r ⊆ A, and therefore uv ∈ B, as required.
4
(b) Again, a natural way to solve this problem is to give an NFA for C. Let us assume
M = ( Q, Σ, δ, q0 , F )
is a DFA for which L( M) = A, like we did above. This time our NFA will be slightly more
complicated. In particular, let us define
N = ( P, Σ, η, p0 , G )
as follows. First, we will define
P = {0, 1} × Q × Q ∪ { p0 },
for p0 being a special start state of N that is not contained in {0, 1} × Q × Q. The accept states of
N will be
G = {(1, q, q) : q ∈ Q}.
It remains to describe the transition function η of N, which will be as follows:
(i) η ( p0 , ε) = {(0, q, q) : q ∈ Q}.
(ii) η ((0, r, q), σ) = {(0, δ(r, σ), q)} for all q, r ∈ Q and σ ∈ Σ.
(iii) η ((0, r, q), ε) = {(1, r, q)} for every r ∈ F and q ∈ Q.
(iv) η ((1, r, q), σ) = {(1, δ(r, σ ), q)} for all q, r ∈ Q and σ ∈ Σ.
All other values of η that have not been listed are to be understood as ∅.
You might look at this definition and have no idea how N works, so let’s consider it in more
detail. N starts out in the start state p0 , and the only thing it can do is to make a guess for some
state of the form (0, q, q) to jump to. The idea is that the 0 indicates that N is entering the first
phase of its computation, in which it will read a portion of its input string corresponding to v in
the definition of C. It jumps to any state q of M, but it also remembers which state it jumped to.
Every state N ever moves to from this point on will have the form ( a, r, q) for some a ∈ {0, 1} and
r ∈ Q, but for the same q that it first jumped to; the third coordinate q represents the memory of
where it first jumped, and it will never forget or change this part of its state. Intuitively speaking,
the state q is a guess made by N for the state that M would be on after reading u (which N hasn’t
seen yet, so it’s just a guess).
Then, N starts reading symbols and essentially mimicking M on those input symbols—this is
the point of the transitions listed as (ii). At some point, nondeterministically chosen, N decides
that it’s time to move to the second phase of its computation, reading the second part of its input,
which corresponds to the string u in the definition of C. It can only make this nondeterministic
move, to a state of the form (1, r, q), when r is an accepting state of M. The reason is that N only
wants to accept vu when M accepts uv, so M should be at an accepting state at this point. This
is the point of the transitions listed as (iii). Finally, in the second phase of its computation, N
simulates M on the second part of its input, which corresponds to the string u. It accepts only for
states of the form (1, q, q), because those are the states that indicate that N made the correct guess
on its first step for the state that M would be in on input u.
This is just an intuitive description, not a formal proof. It is the case, however, that L( N ) = C,
as a low-level, formal proof would reveal, which implies that C is regular.
5
Again, there is another solution along the same lines as the second solution above to part (a).
This time it’s actually a much easier solution, at least in my opinion. Let M be a DFA for A,
precisely as above, and define A p,q for each p, q ∈ Q as in the second solution to part (a). The
language
[
A p,r Aq0 ,p
( p,r )∈ Q× F
is regular, again by the closure of the regular languages under finite unions and concatenations. It
therefore suffices to prove
[
C=
A p,r Aq0 ,p .
( p,r )∈ Q× F
By definition, every element of C may be expressed as vu for u, v ∈ Σ∗ satisfying uv ∈ A. Let
p ∈ Q and r ∈ F be the unique states for which u ∈ Aq0 ,p and uv ∈ Aq0 ,r . It follows that v ∈ A p,r ,
and therefore vu ∈ A p,r Aq0 ,p , implying
[
C⊆
A p,r Aq0 ,p .
( p,r )∈ Q× F
Along similar lines, for any choice of p ∈ Q, r ∈ F, u ∈ Aq0 ,p , and v ∈ A p,r it holds that uv ∈
Aq0 ,p A p,r ⊆ A, and therefore vu ∈ B, from which the inclusion
[
A p,r Aq0 ,p ⊆ C
( p,r )∈ Q× F
follows.
4. [6 points] Let Σ = {0}, and consider the nonregular language
n 2
o
A = 0n : n ∈ N .
Suppose further that B ⊆ Σ∗ is a language for which B ⊆ A. Prove that B is regular if and only
if B is finite.
Solution. We know that every finite language is regular, so it suffices to prove that if B is infinite,
then B is nonregular. The proof will use the pumping lemma.
Assume toward contradiction that B is regular. There must therefore exist a pumping length
p ≥ 1 for B. Let n be any positive integer for which it holds that n ≥ p and
2
0n ∈ B.
There must exist such a choice of n under the assumption that B is infinite—B is a subset of A, so
if there were no such choice of n, then B would necessarily be finite.
Now, it holds that
2
n
0 = n2 ≥ p.
6
and so the pumping lemma implies that it is possible to write
2
0n = xyz
for some choice of x, y, z ∈ Σ∗ satisfying y 6= ε, | xy| ≤ p, and xyi z ∈ B for all i ∈ N. By the first
two requirements, we must have y = 0k for some choice of k ∈ {1, . . . , p}. Taking i = 2, we find
that
2
xyyz = 0n +k ∈ B.
However, this is not possible. Because 1 ≤ k ≤ p ≤ n, we must have
n2 < n2 + k < n2 + 2n + 1 = (n + 1)2 .
As n2 + k lies properly between the two consecutive perfect squares n2 and (n + 1)2 , it cannot be a
perfect square, in contradiction with the assumption that B ⊆ A. Having obtained a contradiction,
we conclude that B is nonregular, as required.
7