\documentstyle[11pt,twoside,psfig]{article}
\input{/a/class/6.045/6045-spring-92/handouts/defs-6045}
\begin{document}

\handout{quiz1solns}{Solutions to Quiz 1}{March 11, 1992}

%\paragraph
\begin{enumerate}
\item

{\bf Problem:} Give a DFA accepting the language 
$L = \{ x \in \{ 0, 1 \}^*:$  every block of three consecutive
symbols of  $x$  contains at least one $0 \}$.
For example, 10110010110 is in $L$, but 1011101001 is not.


{\bf Answer:} Quite a few of you did not know how to treat the case where
$|x|\leq 2$. In order to clarify this point we will first  rewrite what $L$ is.
We will let 
${\cal P_s}(x)$ denote the predicate 
``$[s $ is a sub-block of $x \Rightarrow s $ contains a $0 ]$''.
Then $L$ can be rewritten
as $L = \{ x \in \{0,1\}^* : \forall s \mbox{ such that } |s| = 3 $, the predicate
${\cal P_s}(x)$ is true~$\}$. If $|x|\leq 2$ (in particular if $x= \Lambda$
all the predicates ${\cal P_s}(x)$ are trivially true (for $|s| = 3$) because their
premises are false. This shows that the strings of length at most 2 are in the
language. 

We then construct a DFA $M$ accepting $L$. $M$ keeps track of the  length
of the $1$-substring being currently scanned: if this length reaches 3, the 
machine finds a substring of length 3 containing no 0 and the string is rejected.
Conversely a string is rejected only when this happens. The DFA is shown in 
Figure~\ref{fig-solu:1}.

\begin{figure}[h]
\centerline{\mbox{\psfig{figure=/a/class/6.045/6045-spring-92/handouts/handout18/fig1.ps,height=2in}}}
\caption{DFA accepting $L$ in problem 1}
\label{fig-solu:1}
\end{figure}



%\paragraph{Problem 2.}
\item
{\bf Problem:} Give a regular expression for the language accepted by the NFA 
in Figure~\ref{Fig-1}.
(Missing arrows are assumed to go to a dead state.)
You need not follow the general construction given in class---any way
of obtaining a correct expression is acceptable.

\begin{figure}[h]
\centerline{\mbox{\psfig{figure=/a/class/6.045/6045-spring-92/handouts/handout16/fig1.ps,height=2in}}}
\caption{NFA for Problem 2}
\label{Fig-1}
\end{figure}

{\bf Answer:} The language of problem 2 is described by the regular expression:
$$ (a^* + (a+b) bb )^* (a + b + \Lambda),$$
which expresses that a string $x$ is in  the language exactly when
on input $x$ the given NFA cycles some number of times around
its three states before eventually finishing in one of the two right states.

%\paragraph{Problem 3.}
\item
{\bf Problem:} Consider the 4-symbol alphabet $ \{ A, B, C, D \}$,
in which each symbol represents a two-bit binary word.
To be specific, consider the correspondence $A= {0\choose 0}$, 
$B= {0\choose 1}$, $C= {1\choose 0}$, $D= {1\choose 1}$.
If $x$ is a string over $\{ A, B, C, D \}$, then define
$p(x)$ to be the 
binary number formed by concatenating the upper bits of all the
symbols,
and $q(x)$ to be the number formed by concatenating the lower bits of $x$.
These are interpreted most-significant bit first. For example,
$p(DADB) = 1010$ (binary) $= 10$ (decimal), whereas 
$q(DADB) = 1011$ (binary) $= 11$ (decimal).

Let $L = \{ x \in \{ A, B, C, D \}^* : p(x) + 1 = q(x)\}$.

Thus, a string is in $L$ if the 
binary number formed by concatenating the lower bits of all the
symbols is exactly the sum of 1 and of the binary number formed from the upper
bits of all the symbols.
For example, ``DADB'' is in $L$, since $10+1=11$.

Prove that $L$ is regular.

{\bf Answer:} For any integer $n$ we let bin$(n)$ denote the binary
representation of this integer. We let also $1^k$ (resp. $0^k$) denote
the concatenation of $k$ symbols all equal to 1 (resp. $0$).

{\bf Claim:} $\forall a,b \geq 0, \ b= a+ 1$ if and only if
$\exists k \geq 0, \exists s\in \{0,1\}^*$ such that
bin$(b)  = s 1 0^k$ and bin$(a)  = s 0 1^k$.

This fact just expresses that adding $a$ and $1$ involves $k$ bits of
carry-over.  It could be formally established by induction on $k$.

Note that that  $A$ and $D$ are the alphabet symbols which have their upper and
lower bits equal so that any concatenation $y$ of these letters
is such $p(y) = q(y)$. Hence the previous claim translates into:

 Let $ x \in \{A,B,C,D\}^*$. Then $p(x) +1 = q(x)$ iff
$\exists k \geq 0, \exists y\in \{A,D\}^*$ such that
bin$(b)  = y B C^k$. This means exactly that $L$ is regular and given by
$$L=(A+D)^*BC^*.$$



%\paragraph{Problem 4.} 
\item 
{\bf Problem:} Following the procedure in lecture and the book,
give a DFA equivalent to the  NFA
 shown in Figure~\ref{Fig-2}.

%%Figure 2
%% states = ABCD, start = A, accepting = A
%% 0-edges: (D,C), (C,B), (B,A)
%% 1-edges: (A,B), (A,C), (A,D)

\begin{figure}[ht]
\centerline{\mbox{\psfig{figure=/a/class/6.045/6045-spring-92/handouts/handout16/fig2.ps,width=10cm}}}
\caption{NFA for Problem 4}
\label{Fig-2}
\end{figure}


{\bf Answer:} We will just give here the final result. 
A DFA accepting the language of problem 4 is shown in 
Figure~\ref{fig-solu:4}.  A common mistake was to omit the dead state
or some of the transitions.  Recall that for a DFA, all states must be
shown, and there must be a single transition out of each state for
each character in $\Sigma$.

\begin{figure}[h]
\centerline{\mbox{\psfig{figure=/a/class/6.045/6045-spring-92/handouts/handout18/fig2.ps,height=2in}}}
\caption{DFA accepting $L$ in problem 4}
\label{fig-solu:4}
\end{figure}


%\paragraph{Problem 5.}
\item
{\bf Problem:} Prove that the following language is not regular.
$L = \{ a^i x : i \geq 10, x \in \{ b, c \}^* \mbox{ and } N_b(x) = N_c(x) \}$.


{\bf Answer:} There were several possible ways to solve this problem.
The most straightforward way was to consider the infinite set $S =
\{a^{10}b^i | i \geq 0\}$.  Pick any two different strings from $S$,
$x=a^{10}b^i$ and $y=a^{10}b^j$, where $i \neq j$.  Then the two
strings are pairwise distinguishable, since if we append the string $z
= c^i$ then $xz \in L$, but $yz \not\in L$.  Because there exists an
infinite set of strings such that any pair is distinguishable, there
cannot be a finite-state automata accepting $L$; hence $L$ is
non-regular.

The slickest way to prove this result is to consider the homomorphism
$h(a) = \Lambda$, $h(b) = 0$, $h(c) = 1$, and note that $h(L) = \{ x
\in \{0,1 \}^* | N_0(x) = N_1(x) \}$, which was shown to be
non-regular in Example 8.6 in Martin.  Since if $L$ is regular, $h(L)$
must be regular, it follows that if $h(L)$ is non-regular, then $L$ is
non-regular.  A pumping lemma proof was also possible (though more tedious).

Common mistakes included pumping lemma proofs that did not consider
all possible ways of parsing the chosen string $x$ into $uvw$
(especially the case that $v$ might contain only $a$s), pairwise
distinguishability proofs that only showed that there exists an
infinite number of {\it pairs} of strings that are distinguishable
(true of the regular language $(00)^*$), and proofs that attempted to
conclude that $L$ was non-regular from the fact that it could be
expressed as $L_1 L_2$, where $L_1$ was regular and $L_2$ was
non-regular (which was shown to be an invalid conclusion on the quiz
review problems!).


%\paragraph{Problem 6.}
\item
{\bf Problem:} Let $M$ be a state-output automaton (a Moore automaton):
\[
	M = (Q,\Sigma,T,q_0,\delta,\gamma)\ .
\]
Let $\gamma^*(x)$ denote the output of automaton $M$ when it is supplied
with input $x$.  

State a condition in terms of $Q,\Sigma,T,q_0,\delta$  and $\gamma$ 
characterizing exactly all the Moore automata such that the 
mapping $\gamma^*$ is one-to-one.  
(In other words, give a condition on $M$ such that for any $x$, 
knowing $\gamma^*(x)$ (and $M$), you can determine what $x$ is;
this condition should be the weakest possible such condition.)
Your ``condition on $M$'' can involve $\delta^*$ and $\gamma$, 
but should not involve any reference to $\gamma^*$.
You need not prove that your answer is correct.


{\bf Answer:} We want to say that from any {\em reachable} state $q$,
for any output symbol $y$ there is at most one input symbol $x$ that
will cause the machine to go into a state outputting $y$. Formally:
$$\forall s\in \Sigma^*,\ \forall x, x_1 \in
\Sigma,\ \gamma(\delta^*(sx)) = \gamma(\delta^*(sx_1)) \Rightarrow
x= x_1.$$


%\paragraph{Problem 7.}
\item {\bf Problem:}
\begin{enumerate}
\item Give a CFG to generate the language
$L_1 = \{ x c y : x, y \in \{ a, b \}^*, |x| > |y| \}$.
\item  Give a CFG to generate the language
$L_2 = \{ x c y : x, y \in \{ a, b \}^*, y \neq REV(x) \}$.
\end{enumerate}

{\bf Answer:}
\begin{enumerate}
\item
\begin{eqnarray*}
S &\rightarrow& XS | XW \label{eq7-1}\\
W &\rightarrow& XWX | c \label{eq7-2}\\
X &\rightarrow& a | b \label{eq7-3}
\end{eqnarray*}

\item
The following grammar can be explained in the following way: in order
that $y \neq REV(x)$, either $|x| > |y|$, $|x| < |y|$, or $|y| = |x|$
and $y \neq REV(x)$.  $S_1$ and $S_2$ handle the first two cases, and
are based on the answer for part (a). $S_3$ handles the final case, by
allowing any number of productions in which the same character is
added to the beginning and end in the string.  Then the productions of
$S_3$ involving $W$ break the symmetry and ensure that the string is
different from its reverse. The $W$ productions continue to generate
any arbitrary string for the end of $x$ and the beginning of $y$.  The
most common mistake for this part of the problem was to give the $S_3$
grammar and forget the cases for $S_1$ and $S_2$.

\begin{eqnarray}
S &\rightarrow& S_1 | S_2 | S_3 \nonumber\\
S_1 & \rightarrow & XS_1 | XW \nonumber \\
W & \rightarrow & XWX | c \nonumber \\
X &\rightarrow& a | b\nonumber \\
S_2 & \rightarrow & S_2X | WX \nonumber \\
S_3 & \rightarrow & aS_3a | bS_3b | aWb | bWa \nonumber 
\end{eqnarray}

\end{enumerate}

%\paragraph{Problem 8.}
\item
{\bf Problem:} Show using the pumping lemma for CFL's that the following language is
not context-free:
$L = \{ x\ c\ REV(x)\ c\ x : x \in \{ 0, 1 \}^* \}$.

{\bf Answer:} Assume that the language is a CFL. Then there exists an
$N$ such that $\forall x \in L$ such that $|x| \geq N$ there exists a
partition $x=uvwst$ with $|vw|\geq 1$ such that $\forall m \geq 0,
x=uv^mws^mt \in L$.  Let $x= 1^N c 1^N c 1^N $ that I will denote
$AcBcC$ in order to refer conveniently to the three regions delimited
by the two characters $c$.

Case (a):
If $v$ and $w$ are both
in the same region $A, B$ or $C$ (say $A$) we easily get a contradiction:
the choice of $m=0$ produces a string having less letters in one
region than another (say, in $A$ than in $B$).

Case (b): If $v$ or $w$ contain a $c$, then the same choice $m=0$
brings the total number of $c$s to 0 or 1.

Case (c): The last case is then when  $v$ and $w$ are in two different regions,
say $A$ and $B$. Then the same choice $m=0$ produces a string which has
more letters in $C$ than in (at least) one of the two regions $A$ and $B$.

This case analysis shows that the pumping lemma cannot apply so that
$L$ is not context-free.



%\paragraph{Problem 9.}
\item
{\bf Problem:} Describe in English an algorithm (procedure) for
deciding each of the following.  (Note: The procedures do not have to
be efficient -- they just have to be correct.)
\begin{enumerate}
\item for a given DFA, whether or not it accepts at least two distinct words.
\item for a given CFG, whether or not it generates at least two distinct
words.
\item for a given DFA and natural number n, whether there is an NFA
equivalent to the DFA that has fewer than n states.
\end{enumerate}

{\bf Answer:}
\begin{enumerate}
\item The most straightforward answer to this problem was to
apply the DFA-to-regular expression algorithm to the DFA, and then
check whether the regular expression contained a $~^*$ operator
applied to a non-empty string (which would imply an infinite number of
strings) or the ``+'' of two different strings.  Other acceptable
answers included searching the DFA for two distinct paths to an
accepting state, or checking all strings $x \in \Sigma^*$ of length no
greater than $2N$ to see if two different strings are accepted.  The
reason that checking only these strings will work is the same as is
given on Martin pg. 149 for checking the finiteness of a language: if
the language is infinite, certainly it has more than two distinct
strings, while if it is finite, we have seen all the strings in the
language and can tell if there are two distinct strings.

\item The easiest answer here was to check first whether
$\Lambda \in L(G)$ using the algorithm for the membership problem (as
discussed in class or on Martin pg. 274).  Then convert the grammar to
Chomsky Normal Form, and check the membership of all strings of length
no greater than $2 \cdot 2^{n+1}$, where $n$ is the number of
variables in the CNF grammar.  Once again, this procedure works for the same
reason that checking the finiteness of a context-free grammar works
(see Martin Pg. 275).

A common mistake was to try simple-minded checks on the structure of the
grammar; for example, whether $S \rightarrow A | B$.  The problem with
this approach was that most people did not state how to check whether
$A$ and $B$ would generate distinct strings.

\item Many people claimed that that there could be no NFA
smaller than the minimum-state DFA, which is incorrect.  Since we have
no way to minimize an NFA similar to that for minimizing a DFA, the
best answer was to simply try all possible NFA's with fewer than $n$
states. To check whether a particular NFA is equivalent to the given
DFA, we convert the NFA to a DFA and compare the two minimum-state
versions of the DFAs to see if they are isomorphic.  There are a lot
of NFAs to check, but there are certainly a finite number of them (we
said you didn't have to be efficient!).

\end{enumerate}

%\paragraph{Problem 10.}
\item
{\bf Problem:} Prove or disprove the following: ``The intersection of
an infinite number of regular languages is a regular language''.

{\bf Answer:} The statement is false.  There were two good ways to
solve this problem.  The first was to come up with a particular
counterexample.  For instance, let $L_{ij} = \Sigma^* - \{0^i1^j\}$.
Certainly $L_{ij}$ is regular because it is the minus of two regular
languages.  Consider $\cap_{i=0}^{\infty} \cap_{j \neq i} L_{ij}$.  If
the intersection of an infinite number of regular languages is
regular, the for any particular $i$, $\cap_{j \neq i} L_{ij}$ is
regular; thus $\cap_{i=0}^{\infty}
\cap_{j \neq i} L_{ij}$ must also be regular.  But $\cap_{i=0}^{\infty}
\cap_{j \neq i} L_{ij} = \{0^i 1^i | i \geq 0 \}$ which we know is
non-regular.  Thus the intersection of an infinity of regular
languages is not always regular.

The other way to prove this was to remember from recitation that the
union of an infinite number of regular languages is not always regular
(e.g. $L_i = \{0^i 1^i\}$; $\cup_{i=0}^{\infty} L_i = \{0^i 1^i | i
\geq 0\}$).  If the infinite intersection of regular languages
is always regular, then by DeMorgan's Law, $\cup_{i = 0}^{\infty} L_i
= \overline{\cap_{i = 0}^{\infty} \overline{L_i}}$ is always regular, which is a
contradiction.

\end{enumerate}
\end{document}






