An Overview of Turing Machines
Published: 2020-01-10 19:25:06

The famous mathematician Alan Turing introduced the <strong>abstract concept</strong> of a Turing machine as a part of his research on a problem that was introduced in 1928 by David Hilbert called Entscheidungsproblem. The importance of Turing machines arises because they're one of the first theoretical models for computers and, consequently, a number of theories and ideas in the computer science field were based on this concept. In this post, I'm going to walk you through Turing Machines' concept.

There are multiple versions of Turing machines, but the most popular and intuitive one is the standard Turing machine (STM). Throughout this post, the STM will be used.

<h2>Table of Contents</h2>
1. [Informal Definition](#title1)
2. [Formal Definition](#title2)
3. [Transitions](#title3)
4. [Language acceptors VS. transducers](#title4)
5. [Example](#title5)

<h2 id="title1">Informal Definition</h2>

Turing machine has a tape which is infinite from both sides of the input. The following picture visualizes the tape of the input aabbcc:

![Turing machine tape of input string aabbcc](https://thepracticaldev.s3.amazonaws.com/i/cqebfpc2atg27rzxirw4.png)

Cells on each side of the input are empty (i.e. blanks) and denoted by the symbol □.

There's a scanner that scans the cell content and performs the next action based on the scanned input. The next action can be writing in the current cell and then moving left/right. In the initial configuration of the Turing machine, the scanner is set to the leftmost character of the input as the following picture shows:

![Initial configuration of TM with input string aabbcc](https://thepracticaldev.s3.amazonaws.com/i/wf2vxa01v6u8225oyyr0.png)

A Turing machine accepts strings that halt in a final state which merely depends on the scanner (which has a kind of a Finite automaton). <strong>Therefore, we can conclude that the tape contents are irrelevant to the acceptance of strings.</strong>

A Turing machine rejects strings that halt in a non-final states or don't halt at all. In the latter case, the Turing machine runs into an infinite loop because of the input string.

<h2 id="title2">Formal Definition</h2>

The formal definition is easier to understand when it's read simultaneously with a real example. If you feel that you are unable to digest the definition, take a look at the example provided in <a href="#title5">Example section</a>.

A Turing machine M is defined by
M = (Q,Σ,Γ,δ,q<sub>0</sub>, ,F) where
Q is the set of internal states,
Σ is the input alphabet
Γ is the finite set of symbols called the tape alphabet,
δ is the transition function,
□ ∈ Γ is a special symbol called the blank,
q<sub>0</sub> ∈ Q is the initial state,
F ⊆ Q is the set of final states.

In the definition of a Turing machine, we assume that Σ ⊆ Γ – {□}, that is, that the input alphabet is a subset of the tape alphabet, not including the blank. Blanks are ruled out as input for reasons that will become apparent shortly. The transition function δ is defined as δ : Q × Γ → Q × Γ × {L,R}.

<h2 id="title3">Transitions</h2>

δ(q, a) = (q<sub>0</sub>, b,R) means that from state q, if you see a on tape, go to state q<sub>0</sub>, write b on tape, and move one square to the right on the tape.

δ(q, a) = (q<sub>0</sub>, b, L) means same as above, except move left on the tape.

What if δ(q, a) is undefined ? (i.e. δ(q, a) = Φ, there is no move corresponds to the pair of (q,a)). Simply, we say that the Turing machine halts. As discussed earlier, if the machine halts in a final state, it accepts the input string. Otherwise, it rejects the input string.
  
<h2 id="title4">Language acceptors VS. transducers</h2>

A Turing machine that accepts a specific language, similar to finite automata and PDAs, is called language acceptor.

A Turing machine that take an input string and transforms it to an output is called transducers.

<h2 id="title5">Example</h2>

Let's take the language L={a<sup>n</sup>b<sup>n</sup>c<sup>n</sup> | n ≥ 0} to help us understand what the concept of Turing machines is.

Before start trying to tackle this language and design a Turing machine for it, we should understand what string it contains. This language contains all ordered strings such that a's at the beginning of the string, b's at the middle and c's at the end where the number of a's, b's and  c's match.

Our Turing machine should match one a with one b with one c and keep looping until the entire string is matched to accept the string. Here's a visualized of this procedure on the string aabbcc:

![Animation for how the TM should be processed](https://thepracticaldev.s3.amazonaws.com/i/rzhtxobb9an4kmxsk0dx.gif)

In the first iteration, the leftmost "a" will be converted to "A" symbol (can be any other symbol). Then the scanner will skip all small a's and convert the leftmost "b" to "B" symbol. Then the scanner will skip all small b's and convert the leftmost "c" to "C" symbol. After that, the scanner will go left skipping all "C" symbols, b's, "B" symbols and a's until it reaches an "A" symbol and it will go right to start the matching process again. At this point, the scanner will be on the left most "a" (note it's a small "a") and it'll proceeds as the first iteration with a minor difference is that the Turing machine will skip any "B" and "C" symbols. After the scanner finishes matching and goes back to the rightmost "A", it will move one cell to the right which is a cell that contains "B". When the scanner faces "B" symbol after moving on cell right, it's a sign that the string is matched. Then it will move to the right skipping all "B"  and "C" symbols to verify that the b's and c's are all matched. The Turing machine will halt in a final state if after skipping all  "B" and "C" symbols, the scanner reached a blank.

Here's the Turing machine design that accepts the Language L:
![TM design for the language L](https://thepracticaldev.s3.amazonaws.com/i/rzfmzh0pfl69k0910jv3.png)

At the end of this post, I'd recommend a few resources if you'd like to acquire more knowledge about Turing machines: 
1- <a href="https://plato.stanford.edu/entries/turing-machine/">Turing Machines published by Stanford</a>
2- <a href="https://youtu.be/dNRDvLACg5Q">Turing Machines Explained - Computerphile</a>
3- <a href="https://books.google.com.sa/books/about/An_Introduction_to_Formal_Languages_and.html?id=hsxDiWvVdBcC&printsec=frontcover&source=kp_read_button&redir_esc=y#v=onepage&q&f=false">An Introduction to Formal Languages and Automata by Peter Linz</a>