UNIVERSITY AT BUFFALO, THE STATE UNIVERSITY OF NEW YORK
The Department of Computer Science & Engineering
cse@buffalo
CSE 111: Great Ideas in Computer Science

Turing Machines

Alan Turing

Alan Turing is often called "The Father of Computer Science." He has contributed major insights to the fields of theoretical computer science and artificial intelligence, not to mention his important work during WWII in breaking the German enigma code.

We'll talk about his influence in AI later in the course, but todays lecture is based on some of his contributions to theoretical computer science.

Unfortunately Turing did not live very long - only until age 41. Unlike many other paradigm shifting scientists of old whose ideas prompted their persecution, Turing's sexuality was unfortunately his undoing. In the eyes of the British courts in 1952 he was a pervert for being a homosexual (as it was illegal in Britain until 1967). When he was found guilty his security clearance was revoked and he was forced to stop his cryptoanalysis work for the government. He died of cyanide poisoning in 1954, believed to be suicide.

For more information:

The Turing Machine

A Turing Machine is a conceptual model of a computer. It is able to do everything the computers we use every day are able to do, but the model is meant to be simpler. Instead of having to talk about a specific architecture of a computer (whether it be our calculator, or desktop computer, or something else) we can just talk about this single simple model. It is used often by theorists in proving things about what problems are computable. It is widely believed that for every algorithm there exists a Turing Machine which computes it.

The formalization of a Turing Machine we will use is:
Rapaport, William J. (1985), "Turing Machines" [PDF], from Morton L. Schagrin, Randall R. Dipert, & William J. RapaportLogic: A Computer Approach (New York: McGraw-Hill, 1985): 327-339.

I will describe it in part here, but you should read the above paper for the full treatment. Some of the diagrams in this section are from that text.

Turing Machines (TMs) have a lot in common with the Finite Automatas we've already discussed. A TM has a finite control unit which works much like a FA, changing states based on what the tape is reading. The state register keeps track of what the state currently is. The big difference from an FA is that the machine reads and writes to a (at least conceptually) infinite tape. What we mean by this is that we can always add to the tape - we never run out. We can check whether we are at the beginning or end though of the tape that exists, and cut off a piece from the end. We can change values and add things to the tape as well. The TM therefore has memory and can perform tasks producing output, which an FA cannot do.

The main limitation to this is that the TM can look at only one "square" on the tape at once. Each square of the tape contains either a 1 or 0 when we begin (this is the input to the machine), and with this and the pre-programmed algorithm we have to produce output on that same tape.

Our TM is thought of as a machine on wheels, with a paper tape stretched across the top of the machine. The machine has a scanner which looks at one square of the tape at a time. It's able to read a 1 or 0 from the tape, and also write a 1 or 0. Inside our TM is some programming which tells it what to do when it sees a 1 or 0. Upon each input it switches states (which we can think of as a mental state of mind in a human). It also has a state register, which just tracks which state the machine is currently in.

The programs (algorithm) the TM is capable of executing are also quite limited. They contain only 5 kinds of instructions:

  1. START
  2. IF the current state is P, and symbol S is being scanned, THEN:
    1. Change the symbol on the tape from S to S'
    2. Change the current state to Q
  3. IF the current state is P, and symbol S is being scanned, THEN:
    1. Move one square to the right
    2. Change the current state to Q
  4. IF the current state is P, and symbol S is being scanned, THEN:
    1. Move one square to the left
    2. Change the current state to Q
  5. STOP

Very crucially these instructions are enough to do the sequence, selection, and loop we have talked abut in class. Even more importantly though, it was shown in the Church-Turing thesis that EVERYTHING that can be computed, can be computed using a Turing Machine!

We can draw a program for the TM using a flowchart (similar to the decision trees we've discussed previously).

The flow chart contains 3 types of blocks:

  1. Control blocks (START and STOP), drawn as rounded rectangles. These represent the START and STOP instructions above.
  2. Decision blocks (0? and 1?), drawn as diamonds. These represent the tests for whether the currently read square contains 1 or 0.
  3. Instruction blocks (RIGHT, LEFT, PRINT-0, PRINT-1, ERASE).

We'll also add two more decision blocks to test whether we are at the LEFTEND or RIGHTEND of our tape. Since our tape is assumed to be as long as we need it to be, moving past the end adds another square to our tape. If we need to remove a square from the tape, we use ERASE.


That specifies the machine. That's really it - with this we now have the ability to do computation!

Also, there are many TM simulators on the internet. This one seems particularly good, and I recommend using it (or one like it) when writing your own Turing machine code to test it.

Some Turing Machine Programs

Let's start with a machine which negates some binary input. That is, if the input is 110, the machine's tape would contain 001 after the program has executed.

Lets start by defining a program for our machine to execute.

  1. START.
  2. IF you are in M0 and are scanning 0,
    1. PRINT-1
    2. Change state to M1
  3. IF you are in M0 and are scanning 1,
    1. PRINT-0
    2. Change state to M1
  4. IF you are in M1 and you are at RIGHTEND, THEN STOP
  5. IF you are in M1 and you are not at RIGHTEND,
    1. RIGHT
    2. Change state to M0

The state M0 handles actually changing the value of the square we're currently looking at. The state M1 determines if we are done yet. If we aren't, we go back to M0. These programs are much easier to see and understand when in flowchart form:

In-Class Exercise: Create a TM which on its tape when it starts is only a single symbol (1 or 0). Make your machine write the negation of the single symbol in the square to its right. It's probably easier to draw the flow chart before the program.

Our next TM will be one which determines the truth value of two binary digits after taking the disjunction (OR) of them.

  1. START.
  2. IF you are in M0 and are scanning 0,
    1. RIGHT
    2. Change state to M1
  3. IF you are in M0 and are scanning 1,
    1. RIGHT
    2. Change state to M2
  4. IF you are in M1 and are scanning 1,
    1. RIGHT
    2. Change state to M3
  5. IF you are in M1 and are scanning 0,
    1. RIGHT
    2. Change state to M4
  6. IF you are in M2,
    1. RIGHT
    2. Change state to M3
  7. IF you are in M3, PRINT-1 THEN STOP.
  8. IF you are in M4, PRINT-0 THEN STOP.

The idea is that M0 checks the first square. If it's a 1 we already know our OR will succeed, we move right and go to M2 which just moves right again and puts us in M3 (which prints the 1 and ends the program). If M0 was a 0, we depend on the value of the second square. So we move right and are in state M1. If M1 is a 1, the answer is true and we switch to M3 again. If it is a 0, the OR is false, so we go to M4 which prints a 0 and stops.

In-Class Exercise: Create a TM and flowchart for the conjunction (AND) of two binary digits. Write the result as a single digit after the input, as we did above.


The Universal Turing Machine

Turing's most important achievement in regards to Computer Science was not necessarily the TM formalism itself, but rather the Universal Turing Machine. The standard TM is "hard-wired" to compute a certain task (such as negation or addition). Turing showed that one could build a TM which did not have to be hard-wired for a specific task, and could "load a program" (say, in binary) from the tape of the machine, then the machine would act as if it were hard-wired to compute this task. This was a great achievement, and is the basis of modern day computers!

It may seem crazy, but people have actually built Turing Machines! Take a look at these:


Copyright © 2011 Daniel R. Schlegel