Turing Machines: Examples

CS390, Spring 2024

Last modified: Jan 3, 2023
Contents:

Abstract

Practice designing and working with Turing machines.

1 Automat

  1. Review the Turing machines section of the Automat help pages.

  2. Construct the TM from examples 8.2/8.3. Use it to solve Exercise 8.2.1.

  3. Construct your own Turing machine to solve Exercise 8.2.2a. (Note that this language is not a CFL.)

2 New Ways to Solve Old Problems

2.1 Contains 101

 

We have previously designed this FA to accepts strings that contain 101.

Design a Turing machine for the same language

Reveal

2.2 Ends with 101

 

Here is a FA for accepting strings that end with 101.

In this automaton, we can enter the accepting state many times (e.g., 101010) but only accept the string if we are in the accepting state AND have processed all of the input.

Design a TM to accept the same language.

Reveal

2.3 $0^n1^n$

 

Now let’s consider a context-free language.

Consider the language of all strings of the form $0^n1^n$, i.e., some number of zeros followed by an equal number of ones. This is a typical “counting” problem for a PDA, as shown to the right.

Design a TM for this language.

Reveal

The TMs in this section illustrate a number of “programing style” tips that are worth pointing out to people new to Turing machines:

Use a combination of inspection and of running test cases on each of the following Turing machines to determine what it does.

2.4 Recognizing the Language Accepted by a TM

 

What language does this TM accept?

 
Reveal

2.5 Recognizing the Function Computed by a TM

Often we think of TMs not so much as acceptors of languages as computers of functions. The input to the function is the initial content of the tape and the output is the final content of the tape when the TM reaches an accepting state.

 

What is the function computed by this TM when presented with inputs over the language {a,b,A,B}* ?

 
Reveal

2.6 TMs as Functions 2

 

What is the function computed by this TM?

 
Reveal

For example, suppose that we had a string over ${a,b}^*$ and that we wanted to erase the first ‘a’. How would you write a TM to do this?

Reveal

You may notice a symmetry in our shift-left pattern between the way that ‘a’ and ‘b’ are handled. If we wanted to do a shift-left for a language over three symbols instead of two, we would add another branch similar to $q_0 \rightarrow q_1 \rightarrow q_4$ and $q_0 \rightarrow q_2 \rightarrow q_4$. For a language over four characters, we would add yet another branch. Languages with larger alphabets add more states and transitions to this pattern, making everything just a little bit messier and hard to read.

A shortcut discussed in your text and supported by Automat is to store one character in a “variable”, allowing it’s retrieval later. For example, a transition with an input labelled “a,b}w” means “accept input ‘a’ or ‘b’, storing whichever you actually see in the variable w. Any later transition that mentions ”w" is then assumed to be referring to the variable.

How would you use that to simplify our “delete-the-1st-a” TM?

Reveal

2.7 TMs as Functions 3

 

What is the function (over the alphabet $\{a,b,c\}$) computed by this TM?

Hints:

 
Reveal

These TMs illustrate some more common programming tricks common to TMs:

3 Turing Machines as Language Acceptors

Earlier we saw ways to use TMs to accept languages that we had seen with earlier, less powerful automata.

Next, we can consider problems that could not be solved using the automata we have had before.

3.1 $0^n1^n2^n$

Design a TM to recognize the language of strings of the form $0^n1^n2^n$.

(Although $0^n1^n$ is a CFL and can be recognized by a pushdown automaton, $0^n1^n2^n$ is not context-free and requires a more powerful approach.)

Reveal

3.2 $\alpha c\alpha$ where $\alpha \in \{a,b\}*$

3.2.1 Basic Implementation

Consider the problem of designing a TM to compare two strings over $\{a,b\}$ to see if they are equal.

All input to the TM must be on the tape, so we could choose to separate the two strings by a blank, e.g.,

aba abb

or by a separator character

abacabb

I’m going to choose the latter.

Another way to view this machine is to say that it recognizes strings over {a,b,c} of the form

\[ \{ \alpha c\alpha | \alpha \in \{a,b\}* \}, \]

which is definitely not a CFL.

Design a TM to recognize this language:

Reveal

3.2.2 Using Multiple Tapes

We can get an even simpler (IMO) TM by using multiple tapes.

Solve the same problem using a multi-tape TM.

Reveal

4 Turing Machines as Functions

As computer scientists, we are familiar with the fact that numbers and strings of symbols can be encoded in binary (base-2).

Arithmetic in Turing machines is often conducted in an even simpler form: unary encoding, where a single symbol is used (either ‘0’ or ‘1’) and the value of the number is indicated by the length of the string. For example, the decimal number 4 is 100 in binary, and 1111 in unary. The decimal number 6 is 110 in binary, but 111111 in unary.

Unary encoding is often employed for TMs simply because it makes many elementary arithmetic operations nearly trivial

4.1 Unary Form Integer Increment

Suppose that a tape contains an integer $k$ in unary form (i.e., a string of 1’s, where the value of the integer is the length of the string. Construct a TM to replace its input by the value of the function $f(k) = k+1$.

Reveal

4.2 Unary Form Integer Addition

Suppose that a tape contains pair of integers $m, k$ in unary form separated by a single ‘x’. Construct a TM to replace its input by the value of the function $f(m,k) = m+k$.

Reveal

4.3 Binary Addition - Multitape

The convenience of unary does not mean that we can’t do binary arithmetic in TMs.

Suppose that we have two binary integers on a tape, separated by a symbol ‘c’. Design a TM to compute the sum of those two integers.

This will be easiest to do with a multi-tape TM. As in our previous multi-tape example, we will start by copying the first string/number from tape 1 to tape 2, then position the heads of both tapes on the right-most symbol of each string.

After that, we can enact a binary addition, working right to left, in much the way that you would do manually.

For example, given the input tape:

1 1 c 1 1 0

we will split the two inputs onto two tapes like this.

1 1 0
1 1

We will compute the sum and put it onto the first tape:

1 0 0 1
(don’t care)
Reveal