# Solutions to Practice Second Midterm Exam

#### 7 April 2008

Exam text is in black, solutions in blue.

A few inconsequential typos on the exam were corrected on 6 April.

• Question 1 (10): (True/false with justification) There exists a language X such that X = L(M) for some three-tape Turing machine M, but X is not the language of any two-tape Turing machine.

FALSE. Both every three-tape TM and every two-tape TM have an equivalent one-tape machine, equivalent in the sense of having the same language. So if X = L(M) and M has three tapes, X is also the language of a one-tape machine (and therefore of a two-tape machine as well).

• Question 2 (10): (True/false with justification) If A and B are two languages such that A ≤m B and B ≤m A, then A is TR if and only if B is TR.

TRUE. We know that the class of TR languages is closed downward under ≤m, so if A is TR and B ≤m A we know that B is TR. Similarly, if B is TR and A ≤m B we know that A is TR. So if one of the languages is TR, the other must be as well.

• Question 3 (10): (True/false with justification) The language {(M,G): M is a DFA, G is a CFG, and L(M) = L(G)} is TD.

FALSE. We know that ALLCFG, the set of CFG's whose language is Σ*, is undecidable. If the language of this question were decidable, we could determine whether any grammar Y is in ALLCFG by testing whether (Mall, G) is in this language, where MALL is some DFA that always accepts. So this language cannot be TD.

• Question 4 (10): (True/false with justification) The set of all Turing machines that accept themselves is not a TR language.

FALSE. We can construct a machine Z so that on input M, a Turing machine, Z runs M on itself and halts if and only if M accepts itself. L(Z) is the set in question, which is thus TR. (It is not TD because the set of machines that do not accept themselves is not TR.)

• Question 5 (20): This question asks you to describe two deterministic two-tape Turing machines. In each case be somewhat specific about what the machine writes on each tape and how it decides what to write.
• (a,10) Describe a deterministic two-tape Turing machine M such that L(M) = {an: n is prime}.

M never changes its first tape, which holds the input an. It begins by rejecting if n is 0 or 1, and accepting if it is 2. Otherwise it places two a's on its second tape. Then it puts each head at the left end of the string of a's on its tape. It marks off copies of the tape-2 string on the tape-1 string by moving the heads together until the tape-2 head is at the end of its string, then moving the tape-2 head back to the left end and repeating. If it reaches the end of the tape-1 string at the same time it reaches the end of the tape-2 string, it rejects (in this case n must be even).

Otherwise it adds a third a to the tape-2 string, and marks off copies of the tape-2 string on the tape-1 string. If it finds they are equal length, it accepts -- otherwise it marks off copies and rejects if it finds the right ends of the two strings at the same time. Otherwise it continues increasing the size of the tape-2 string by one letter each time and marking off. It accepts if it first finds the tape-1 and tape-2 strings to be the same length, and rejects if it first finds the tape-1 length to be a proper multiple of the tape-2 length.

• (b,10) Describe a deteministic two-tape Turing machine such that L(M) = {w ∈ {0,1}*: w represents a prime number in binary} (Hint: Use your solution to part (a).)

We convert the binary string w, which denotes some number n, to the string an as follows. We form a string of a's to the right of w on tape 1 -- each time we add an a to this string we go to w and alter it to subtract 1 from its number (if the rightmost bit is a 1 we change it to a 0 and stop -- otherwise we find the rightmost 1 (if any), change it to a 0, and change all the 0's to the right of it to 1's). If we find the binary string to be all 0's, we know that the string of a's is an.

This done, we simulate the machine of part (a) on the string an and accept if and only if it finds n to be prime.

• Question 6 (30): Two strings u and v over the same alphabet Σ are called anagrams if for every letter a in Σ, the number of a's in u equals the number of a's in v.
• (a,10) Prove that the language {(u,v): u and v are anagrams} is TD for any alphabet Σ.

Repeatedly (1) cross off the first remaining letter of u and (2) find a copy of the same letter in v and cross it off, until one of u or v is the empty string. If at that time the other string is empty, accept. If it is not empty, or if you failed to find a matching letter at some point, reject.

• (b,20) Define the anagram Post Correspondence Problem or APCP to be the set of PCP dominoes P such that there exists a nonempty sequence of dominoes from P where the string made from the tops of each domino and the string made from the bottoms of each domino are anagrams. For Σ = {0,1}, prove that APCP is Turing decidable.

Associate a vector of two integers (a,b) with each domino: a is the number of 0's on top minus the number of 0's on the bottom, and b is the number of 1's on the top minus the number of 1's on the bottom. We have an anagram-match if and only if there is some sequence of dominos whose vectors of integers add up to (0,0). If any domino has (a,b) = (0,0) we have an anagram-match using one copy of that domino, and we accept. If we have one domino or set of dominos with total vector (a,0), with a > 0 and another with vector (c,0) with c < 0, we have an anagram-match with c copies of the first and a copies of the second. So we need to find out what vectors with second element 0 we can make. For every pair of vectors (a,b) and (c,d) with b > 0 and d < 0, we can make (ad-bc,0) with d copies of (a,b) and b copies of (c,d). If this process makes two vectors of type (+,0) and (-,0), we have an anagram-match. Otherwise these pairs make vectors only of type (+,0), or only of type (-,0). In this case we need to argue that no anagram-match exists. This is because if, for example, all the values of ad-bc are positive, this means that the leftmost vector above the x-axis and the leftmost vector below it combine to give a positive value of ad-bc, and every other sum of vectors is to the right of this and also has a positive x-value if it sums to a point on the x-axis. This problem was much too hard for an exam -- I was thinking that it was sufficient to get a vector sum of the form (a,a), but you need to get (0,0).

• Question 7 (30): Consider the following three languages -- in each case M and N represent binary representations of Turing machines with binary input alphabet:
• AND-PAIR = {(M,N): M ∈ L(N) ∧ N ∈ L(M)}
• OR-PAIR = {(M,N): M ∈ L(N) ∨ N ∈ L(M)}
• IFF-PAIR = {(M,N): M ∈ L(N) ↔ N ∈ L(M)}

• (a,15) Two of these three languages are TR. Say which two are TR and prove that each is TR. (The third is not TR, but you need not prove that.)

AND-PAIR and OR-PAIR are TR. We can make a machine M1 that on input (M,N), runs M on N and N on M in parallel. M1 accepts if and only if both these computations accept, so that L(M1) is AND-PAIR. We can make a similar machine M2 that runs the same two computations in parallel and accepts if and only if at least one of them accepts, so that L(M2) = OR-PAIR.

IFF-PAIR is not TR -- here is a proof of that fact that is too hard for a test. We can reduce ETM (which is not TR) to IFF-PAIR. Given a machine Z, we define M that always rejects, and N that ignores its input and runs Z on all possible strings in parallel and accepts if Z accepts any of them. If Z is in ETM, L(N) will be empty and so M will not be in L(N). Since N is not in L(M), the pair (M,N) is in IFF-PAIR. But if any string is in L(Z), then all strings including M are in L(N), but N is still not in L(M) so (M,N) is not in IFF-PAIR. The function f taking Z to this (M,N) is a reduction showing that ETMm IFF-PAIR.

• (b,15) None of these three languages is TD. Pick any of the three and prove that it is not TD.

By letting M = N, we can reduce {M: M ∈ L(M)} to either AND-PAIR or OR-PAIR. This language is not TD -- if it were its complement LBOS = {M: M is not in L(M)} would be TR, and we know it isn't. If LBOS were the language of some machine B, we would have B in L(B) if and only if B were not in L(B), a contradiction.

If we let M be an always accepting machine, we guarantee that any N is in L(M). If we let Z be any machine and w any string, we can define NZ,w to be a machine that ignores its input and runs Z on w. So M is in L(NZ,w) if and only if (Z,w) is in ATM. The reduction taking (Z,w) to (M,NZ,w) thus proves that ATMm IFF-PAIR, proving that IFF-PAIR is not TD. (Of course the harder argument above showing that IFF-PAIR is not TR also shows that IFF-PAIR is not TD.)