6 Turing Machines

Theory of Computation · Unit 6 · 10 hrs

Turing Machines

Exam-focused notes for Turing Machines (Theory of Computation, CSC262): what the TU syllabus asks and how it has actually been tested, with 12 solved past questions from this unit.

Turing Machines in short

This unit introduces the Turing machine, the most powerful machine model in this subject and the formal definition of "algorithm" on which computability theory rests, covers its variants (multi-tape, multi-track, non-deterministic, restricted), and closes with how a Turing machine's own description can be encoded as data for another Turing machine to read.

Turing Machines: notes

Introduction to Turing Machines (TM)

A Turing machine (TM) extends the pushdown automaton's single stack into a full, unbounded read-write tape: an infinite sequence of cells, each holding one tape symbol, over which a tape head can move one cell left or one cell right at each step, reading the symbol under it and optionally overwriting it before moving. Unlike a PDA's stack, which only ever exposes its top symbol, a Turing machine's head may revisit and rewrite any cell it has already passed, which is exactly the extra freedom needed to accept languages beyond the context free languages. The tape is initially blank except for the input string, and the special blank symbol, conventionally $B$ or $\sqcup$, fills every other cell; the finite control (the machine's current state) behaves like the state of an FSM or PDA, except that a Turing machine is normally required to have a transition defined for every state and every possible tape symbol.

Notations of Turing Machine

Formally, a Turing machine is a 7-tuple $$T = (Q, \Sigma, \Gamma, \delta, q_0, B, F),$$ where $Q$ is the set of states, $\Sigma$ is the input alphabet, $\Gamma \supseteq \Sigma$ is the (possibly larger) tape alphabet including the blank $B \notin \Sigma$, $q_0$ is the start state, and $F$ is the set of final states (commonly split into an accept state and a reject state). The transition function has signature $$\delta: Q \times \Gamma \to Q \times \Gamma \times {L, R},$$ so each rule specifies, for a state and a tape symbol read, the new state, the symbol to write in place of the one read, and the direction (left or right) to move the head one cell; if the head would move left off the very start of the tape, convention has it simply stay put. A transition table lists exactly these four columns, state, symbol read, symbol written plus direction, and next state, one row per defined move.

Language of a Turing Machine

For a Turing machine $T=(Q,\Sigma,\Gamma,\delta,q_0,B,F)$, the language of $T$, written $L(T)$, is the set of strings over $\Sigma$ that cause $T$, started with the string on its tape and the head at the leftmost symbol, to eventually halt in an accepting state: $$L(T) = { w \in \Sigma^* : q_0w \vdash^* \alpha p \beta \text{ for some accepting state } p }.$$ Unlike a DFA, which is always total and finishes reading every input, a Turing machine may simply run forever on some inputs without ever halting; such an input is neither accepted nor rejected, it is undefined, and languages with this property are exactly the recursively enumerable but not recursive languages discussed further in unit 7.

Instantaneous Description for Turing Machine

The configuration of a Turing machine at any point in a computation is captured by an instantaneous description (ID), written as a string $x_1x_2\cdots x_{i-1},q,x_i\cdots x_n$, where $x_1\cdots x_n$ is the (finite, non-blank-bounded) content of the tape, $q$ is the current state, and $q$ is written immediately to the left of the symbol currently under the head, $x_i$. A move $\delta(q,x_i)=(p,y,R)$ is written $x_1\cdots x_{i-1}qx_i\cdots x_n \vdash x_1\cdots x_{i-1}y,p,x_{i+1}\cdots x_n$, moving $p$ one position to the right of where $q$ was and replacing $x_i$ with $y$; a move to the left shifts $q$'s position one symbol left instead. As with the PDA's $\vdash$ relation, $\vdash^*$ denotes zero or more moves, describing an entire run as a single chain.

Acceptance of a string by a Turing Machines

A Turing machine accepts a string $w$ if, started on $w$, it eventually halts with its state in the designated accepting state; it rejects $w$ if it eventually halts in a designated rejecting state (or, in a simpler convention, if it halts anywhere that is not accepting, or if it reaches a configuration with no move defined at all). A Turing machine may also loop forever on some input, neither accepting nor rejecting it, a genuinely new possibility that has no counterpart in a DFA, NFA, or PDA, all of which always finish processing any input in a bounded number of steps.

Turing Machine as a Language Recognizer

As a worked example, a TM can be built to recognize $L = {0^n1^n : n \geq 1}$ by repeatedly crossing off one $0$ and the corresponding $1$. Using states ${q_0,q_1,q_2,q_f}$ and tape alphabet ${0,1,X,Y,B}$:

State$0$$1$$X$$Y$$B$
$q_0$$X,R,q_1$$Y,R,q_0$$B,R,q_f$
$q_1$$0,R,q_1$$Y,L,q_2$$Y,R,q_1$
$q_2$$0,L,q_2$$X,R,q_0$$Y,L,q_2$
$q_f$

State $q_0$ crosses off the leftmost unmarked $0$ (writing $X$) and hands off to $q_1$, which skips right over remaining $0$s and already-crossed $Y$s to find the leftmost unmarked $1$, crossing it to $Y$; state $q_2$ then walks back left to the marker $X$ so $q_0$ can repeat with the next $0$. Tracing "$0011$": $q_0$ marks the first $0$ to $X$ and hands to $q_1$; $q_1$ skips the second $0$ and crosses the first $1$ to $Y$; $q_2$ walks back to the $X$; $q_0$ marks the second $0$ to $X$; $q_1$ skips the already-marked $Y$ and crosses the last $1$ to $Y$; $q_2$ walks back; $q_0$ skips both $Y$s and reads the trailing blank, accepting in $q_f$. Tracing "$011$" (an unbalanced string): after the single $0$ is crossed and matched to a $1$, $q_0$ is left facing the second, unmatched $1$, for which no transition is defined, so the machine halts without accepting, correctly rejecting.

What this unit covers

  • Introduction to Turing Machines (TM)
  • Notations of Turing Machine
  • Language of a Turing Machine
  • Instantaneous Description for Turing Machine
  • Acceptance of a string by a Turing Machines
  • Turing Machine as a Language Recognizer
  • Turing Machine as a Computing Function
  • Turing Machine with Storage in its State
  • Turing Machine as a enumerator of stings of a language
  • Turing Machine as Subroutine
  • Turing Machine with Multiple Tracks
  • Turing Machine with Multiple Tapes
  • Equivalence of Multitape-TM and Multitrack-TM
  • Non-Deterministic Turing Machines
  • Restricted Turing Machines: With Semi-infinite Tape, Multistack Machines, Counter Machines
  • Curch Turing Thesis
  • Universal Turing Machine
  • Turing Machine and Computers
  • Encoding of Turing Machine
  • Enumerating Binary Strings
  • Codes of Turing Machine
  • Universal Turing Machine for encoding of Turing Machine

Encoding of Turing Machine

2081 · 10 marks

Define Turing machine as enumerators of strings of a language. Encode the Turing machine TM = ({q0, q1, q2}, {a, b}, {a, b, B}, δ, q2, B, F) with input w = ba and δ is defined as follows: δ(q0, b) → (q1, b, R), δ(q1, a) → (q2, a, R), δ(q2, a) → (q1, a, R), δ(q2, b) → (q2, b, L)[10]

Show solved answer

Turing Machine as Enumerator and Encoding

Part 1: Turing Machine as an Enumerator

A Turing machine used as an enumerator is a variant of the standard TM equipped with a work tape and a separate output tape (printer). Instead of taking an input and accepting/rejecting it, an enumerator $E$ starts on a blank tape and generates (lists) the strings of a language.

Working principle

  • $E$ starts with all tapes blank (no input is provided).
  • It computes, occasionally printing a string on the output tape followed by a separator symbol #.
  • The set of all strings printed constitutes the language enumerated by $E$:

$$L(E) = {, w \mid E \text{ eventually prints } w \text{ on the output tape} ,}$$

  • Strings may be printed in any order and may repeat.

Key theorem

A language $L$ is recursively enumerable (Turing recognizable) if and only if some enumerator $E$ enumerates it. Hence enumerators and recognizers define the same class of languages.


Part 2: Encoding the Given Turing Machine

Given data (extracted)

  • States: $Q = {q_0, q_1, q_2}$
  • Input alphabet: $\Sigma = {a, b}$
  • Tape alphabet: $\Gamma = {a, b, B}$
  • Start state: $q_2$
  • Blank: $B$
  • Final states: $F$ (not explicitly listed in question)
  • Input: $w = ba$
  • Transitions:
    • $\delta(q_0, b) \to (q_1, b, R)$
    • $\delta(q_1, a) \to (q_2, a, R)$
    • $\delta(q_2, a) \to (q_1, a, R)$
    • $\delta(q_2, b) \to (q_2, b, L)$

Note: the set $F$ of final states is not given, so it cannot be encoded. The encoding below covers states, symbols and transitions, which is what the data supports.

Step 1: Encode states (unary in 1's)

StateCode
$q_0$$1$
$q_1$$11$
$q_2$$111$

Step 2: Encode tape symbols

SymbolCode
$a$$1$
$b$$11$
$B$$111$

Step 3: Encode directions

DirectionCode
$L$$1$
$R$$11$

Step 4: Encoding scheme

Each transition $\delta(q, s) \to (q', s', D)$ is encoded (fields separated by single $0$) as:

$$\text{code}(q),0,\text{code}(s),0,\text{code}(q'),0,\text{code}(s'),0,\text{code}(D)$$

T1: $\delta(q_0, b) \to (q_1, b, R)$ $$1,0,11,0,11,0,11,0,11$$

T2: $\delta(q_1, a) \to (q_2, a, R)$ $$11,0,1,0,111,0,1,0,11$$

T3: $\delta(q_2, a) \to (q_1, a, R)$ $$111,0,1,0,11,0,1,0,11$$

T4: $\delta(q_2, b) \to (q_2, b, L)$ $$111,0,11,0,111,0,11,0,1$$

Step 5: Full encoding (transitions separated by $00$)

$$\boxed{1,0,11,0,11,0,11,0,11 ; 00 ; 11,0,1,0,111,0,1,0,11 ; 00 ; 111,0,1,0,11,0,1,0,11 ; 00 ; 111,0,11,0,111,0,11,0,1}$$

Step 6: Trace on input $w = ba$ (start state $q_2$)

Tape: $B,b,a,B$, head on first symbol $b$, state $q_2$.

  • Config 1: State $q_2$, read $b$. Apply $\delta(q_2,b)\to(q_2,b,L)$: write $b$, move Left. Head now on the blank $B$ to the left of $b$.
  • Config 2: State $q_2$, read $B$. There is no transition for $(q_2, B)$, so the machine halts here.

Since $F$ is not specified, whether this halting configuration is accepting cannot be determined. With the given transitions, the computation halts after one move on the left blank, having never moved right into the $a$. Thus for start state $q_2$ the input $ba$ is not processed to completion in an accepting sense.

(Remark: if the intended start state were $q_0$, the trace would be $q_0 b a \Rightarrow q_1 a \Rightarrow q_2$ (blank) and halt for lack of a $(q_2,B)$ rule. The question explicitly gives start state $q_2$, so the trace above is the correct one for the stated data.)


Final result

  • Enumerator: TM with output tape listing all strings of $L$, equivalent to recursive enumerability.
  • Encoded string of the machine is the boxed binary string in Step 5.
Open this answer on its own page →

2078 · 10 marks

Construct a Turing Machine that accepts the language of odd length strings over alphabet {a, b}. Give the complete encoding for this TM as well as its input string w = abb in binary alphabet that is recognized by Universal Turing Machine.[10]

The TM accepts a string if and only if its length is odd. We use a two-state parity tracking approach: the TM scans the input one symbol at a time, toggling between an "odd count" state and an "even count" state. When it hits a blank (end of input), it accepts if it is in the odd-count state. ---...

Open this answer on its own page →

Acceptance of a string by a Turing Machines

2081 · 5 marks

Turing Machine Analysis

Show solved answer

Turing Machine: Testing String "())))"

Given

Input string: ()))) = ( ) ) ) ) (1 open bracket, 4 close brackets)

Transition Table:

State()XYB
$q_0$X, R, $q_1$---, -, $q_0$ (stay)-, -, $q_4$
$q_1$-X, L, $q_2$-Y, L, $q_2$Y, L, $q_2$
$q_2$--X, R, $q_0$Y, R, $q_3$-, R, $q_4$
$q_3$-, -, $q_3$---, -, $q_3$-, R, $q_4$
  • Start state: $q_0$
  • Accept state: $q_4$
  • $B$ = blank

Note on ambiguity: The table is malformed in the LaTeX. The $q_0$ column headings mix up which symbol triggers each action. The intended machine is the classic balanced-parenthesis TM: on $q_0$ reading ( write X and go right to $q_1$; skip over already-marked X's; halt-accept on blank. The result (accept/reject) does not depend on these cosmetic issues, so I trace using the standard interpretation.


Solution

Initial tape (head at position 1, state $q_0$):

( ) ) ) )
^

Trace

StepStatePosReadActionResulting Tape
1$q_0$1(X, R, $q_1$X ) ) ) )
2$q_1$2)X, L, $q_2$X X ) ) )
3$q_2$1XX, R, $q_0$X X ) ) )
4$q_0$2X(skip right, stay $q_0$)X X ) ) )
5$q_0$3)No transition definedHALT

At step 5, in state $q_0$ the head reads ). The machine needs to find another ( to pair with the remaining ) symbols, but there are none. State $q_0$ has no rule for reading ) (there is no unmarked ( left).

The machine halts without reaching the accept state $q_4$.

Result: STRING IS REJECTED ✗

Reason: The string ()))) has $1$ opening bracket and $4$ closing brackets: it is unbalanced. After the TM matches the single ( with one ), three unmatched ) remain with no ( to pair them. The machine crashes in $q_0$, so ()))) is not accepted.


Transition Diagram

        (→X,R                    )→X,L / Y→Y,L / B→Y,L
   ──►( q0 )───────────────►( q1 )───────────────►( q2 )
        │  ▲                                          │
        │  │      X→X,R (match found, restart)        │
        │  └──────────────────────────────────────────┘
        │
        │ Y→Y,R                                        │ Y→Y,R
        │                                              ▼
        │ B→(no move)                               ( q3 )
        ▼                                              │ B→R
      (( q4 ))  ◄───────── B→R ────────────────────────┘
       ACCEPT
  • $q_4$ (double circle) is the accept state.
  • Acceptance requires reaching $q_4$ on a blank after all brackets are matched.
  • For ()))), the machine never reaches $q_4$.

Conclusion

The string ()))) is REJECTED because it is unbalanced (1 open vs 4 close); the machine halts in $q_0$ with no applicable transition on ).

Open this answer on its own page →

2080 · 10 marks

How does Turing machine accept a string? Design a Turing Machine over the alphabet {0,1,a} that processes the string defined by L = {a01a,a10a,a0101a}. Show both transition diagram and table. Show acceptance of a0101a.[10]

Show solved answer

Turing Machine for L = {a01a, a10a, a0101a}

Given

  • Alphabet (Σ): ${0, 1, a}$
  • Language: $L = {a01a,\ a10a,\ a0101a}$ (a finite language of exactly 3 strings)
  • Tape symbols (Γ): ${0, 1, a, B}$ where $B$ = blank
  • Required: definition of acceptance, TM design, transition diagram, transition table, trace of a0101a

Solution

1. How a Turing Machine Accepts a String

A Turing Machine is a 7-tuple: $$M = (Q, \Sigma, \Gamma, \delta, q_0, B, F)$$

  • $Q$: finite set of states
  • $\Sigma$: input alphabet
  • $\Gamma$: tape alphabet ($\Sigma \subseteq \Gamma$, includes $B$)
  • $\delta: Q \times \Gamma \rightarrow Q \times \Gamma \times {L, R}$: transition function
  • $q_0$: initial state
  • $B$: blank symbol
  • $F \subseteq Q$: set of final (accepting) states

Acceptance process:

  1. The input string $x$ is written on the tape, surrounded by blanks $B$.
  2. The head starts at the leftmost symbol of $x$; machine begins in $q_0$.
  3. At each step the machine reads the current cell, uses $\delta$ to write a symbol, move (L/R), and change state.
  4. A string is accepted if the machine halts in a final state ($q \in F$).
  5. It is rejected if it halts in a non-final state or never halts.

$$L(M) = { x \in \Sigma^* \mid q_0 x \vdash^* \alpha, q_f, \beta,\ q_f \in F}$$

2. Design Strategy

Since $L$ is finite (3 exact strings), the TM scans left-to-right, tracking progress through the shared prefix a, then branching. Since we only recognize, we write back the same symbol and move R.

States:

StateMeaning
$q_0$expect leading a
$q_1$after a; expect 0 or 1
$q_2$after a0; expect 1
$q_3$after a01; expect final a or continue 0
$q_4$after a1; expect 0
$q_5$after a10; expect final a
$q_6$after a010; expect 1
$q_7$after a0101; expect final a
$q_8,q_9,q_{10}$after trailing a; verify blank end
$q_a$accept (final)
$q_r$reject

3. Transition Table

$\delta(\text{state},\text{read}) = (\text{write},\text{move},\text{next})$

StateReadWriteMoveNext
$q_0$aaR$q_1$
$q_0$0,1,B--$q_r$
$q_1$00R$q_2$
$q_1$11R$q_4$
$q_1$a,B--$q_r$
$q_2$11R$q_3$
$q_2$0,a,B--$q_r$
$q_3$aaR$q_8$
$q_3$00R$q_6$
$q_3$1,B--$q_r$
$q_4$00R$q_5$
$q_4$1,a,B--$q_r$
$q_5$aaR$q_9$
$q_5$0,1,B--$q_r$
$q_6$11R$q_7$
$q_6$0,a,B--$q_r$
$q_7$aaR$q_{10}$
$q_7$0,1,B--$q_r$
$q_8$BB-$q_a$
$q_8$0,1,a--$q_r$
$q_9$BB-$q_a$
$q_9$0,1,a--$q_r$
$q_{10}$BB-$q_a$
$q_{10}$0,1,a--$q_r$

4. Transition Diagram

             a         0          1          a         B
  ->(q0) --------> (q1) ----> (q2) ----> (q3) ----> (q8) ----> ((qa))
                     | 1              a01a path |
                     v                          | 0
                   (q4)                         v
                     | 0                       (q6)
                     v                          | 1
                   (q5)                         v
                     | a                       (q7)
                     v                          | a
                   (q9)                         v
                     | B                      (q10)
                     v                          | B
                   ((qa))                       v
                                              ((qa))

(All undefined/unexpected inputs go to $q_r$, reject.)

5. Acceptance of a0101a

Tape: $B\ a\ 0\ 1\ 0\ 1\ a\ B$

StepStateHead readsActionNext
1$q_0$awrite a, R$q_1$
2$q_1$0write 0, R$q_2$
3$q_2$1write 1, R$q_3$
4$q_3$0write 0, R$q_6$
5$q_6$1write 1, R$q_7$
6$q_7$awrite a, R$q_{10}$
7$q_{10}$Bwrite B$q_a$

The machine halts in $q_a$ (final state), so the string a0101a is ACCEPTED. ✓

Open this answer on its own page →

Turing Machine as a Computing Function

2080 · 5 marks

Design a Turing machine that computes a function f(n)=0. [5]

  • Function to compute: $f(n) = 0$ for all $n \in \mathbb{N}$. - Input encoding (standard unary): a natural number $n$ is written as $n$ ones, i.e. tape holds $B,1^n,B$. - Output encoding: result $0$ is represented by $0$ ones, i.e. a blank tape. - Computability criterion (from standard definiti...
Open this answer on its own page →

2080.1 · 10 marks

How Turing Machine is used as a computing function? Construct a TM for simulating a function f(x) = 2x for x = {1}. Iterate the TM for input 11 and generate the output 1111.[10]

  • Function to compute: $f(x) = 2x$ - Input alphabet symbol: $x \in {1}$ (unary representation) - Test input: 11 (i.e., $x = 2$) - Expected output: 1111 (i.e., $f(2) = 4$) - Representation: value $n$ = string of $n$ ones on the tape All data needed to solve is present. --- A Turing Machine (TM) ...
Open this answer on its own page →

Turing Machine as a Language Recognizer

2079 · 5 marks

Construct a Turing machine that accepts the language, $L = {a^n b^n \mid n \geq 0}$ [5]

  • Language: $L = {a^n b^n \mid n \geq 0}$ - Input alphabet: $\Sigma = {a, b}$ - Requirement: construct a TM that accepts (halts in accepting state) exactly the strings with equal numbers of as followed by equal numbers of bs, including the empty string ($n = 0$). No missing data; this is a co...
Open this answer on its own page →

Introduction to Turing Machines

2079 · 5 marks

Define Turing machine and its roles. [5]

A Turing Machine (TM) is a theoretical computational model that consists of an infinite tape divided into cells, a read/write head, and a finite control unit. It is formally defined as a 7-tuple: $$TM = (Q, \Sigma, \Gamma, \delta, q0, B, F)$$ Where: Component Description ------------------------ ...

Open this answer on its own page →

2078 · 5 marks

Define Turing Machine and explain its different variations. [5]

A Turing Machine (TM) is a theoretical computational model that consists of an infinite tape divided into cells, a read/write head, and a finite control unit. It is formally defined as a 7-tuple: $$TM = (Q, \Sigma, \Gamma, \delta, q0, B, F)$$ Where: Component Description ------------------------ ...

Open this answer on its own page →

2076 · 10 marks

Define a Turing machine. Construct a TM that accept $L = { wcw^R \mid w \in {0, 1} \text{ and } c \text{ is } \varepsilon \text{ or } 0 \text{ or } 1 }$. Show that string 0110 is accepted by this TM with sequence of Instantaneous Description (ID). [10]

A Turing Machine (TM) is a theoretical model of computation defined as a 7-tuple: $$TM = (Q, \Sigma, \Gamma, \delta, q0, B, F)$$ Where: Component Meaning -------------------- $Q$ Finite set of states $\Sigma$ Input alphabet (finite set of input symbols) $\Gamma$ Tape alphabet (

Open this answer on its own page →

Turing Machine with Multiple Tapes

2076 · 5 marks

Describe the Turing machines with multiple tape, multiple track and storage in state. [5]

--- A multiple tape Turing Machine is an extension of the standard TM that has k independent tapes, each with its own read/write head. The standard TM is a special case where k = 1. The transition function is extended as: This means: given the current state and the symbols scanned on all k tapes,...

Open this answer on its own page →

Restricted Turing Machines

2080.1 · 5 marks

Describe how multi-stack TM is different from the semi-infinite tape TM? [5]

A Counter Machine is an offline TM whose storage tapes are semi-infinite (the tape extends infinitely in only one direction) and whose tape alphabet contains only two symbols: - Z -- the bottom-of-stack marker, which appears initially on the cell scanned by the tape head and may never appear on a...

Open this answer on its own page →