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.
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 answerHide 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)
| State | Code |
|---|---|
| $q_0$ | $1$ |
| $q_1$ | $11$ |
| $q_2$ | $111$ |
Step 2: Encode tape symbols
| Symbol | Code |
|---|---|
| $a$ | $1$ |
| $b$ | $11$ |
| $B$ | $111$ |
Step 3: Encode directions
| Direction | Code |
|---|---|
| $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.
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]
Acceptance of a string by a Turing Machines
2081 · 5 marks
Turing Machine Analysis
Show solved answerHide answer
Turing Machine: Testing String "())))"
Given
Input string: ()))) = ( ) ) ) ) (1 open bracket, 4 close brackets)
Transition Table:
| State | ( | ) | X | Y | B |
|---|---|---|---|---|---|
| $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
| Step | State | Pos | Read | Action | Resulting Tape |
|---|---|---|---|---|---|
| 1 | $q_0$ | 1 | ( | X, R, $q_1$ | X ) ) ) ) |
| 2 | $q_1$ | 2 | ) | X, L, $q_2$ | X X ) ) ) |
| 3 | $q_2$ | 1 | X | X, R, $q_0$ | X X ) ) ) |
| 4 | $q_0$ | 2 | X | (skip right, stay $q_0$) | X X ) ) ) |
| 5 | $q_0$ | 3 | ) | No transition defined | HALT |
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 ).
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 answerHide 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:
- The input string $x$ is written on the tape, surrounded by blanks $B$.
- The head starts at the leftmost symbol of $x$; machine begins in $q_0$.
- At each step the machine reads the current cell, uses $\delta$ to write a symbol, move (L/R), and change state.
- A string is accepted if the machine halts in a final state ($q \in F$).
- 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:
| State | Meaning |
|---|---|
| $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})$
| State | Read | Write | Move | Next |
|---|---|---|---|---|
| $q_0$ | a | a | R | $q_1$ |
| $q_0$ | 0,1,B | - | - | $q_r$ |
| $q_1$ | 0 | 0 | R | $q_2$ |
| $q_1$ | 1 | 1 | R | $q_4$ |
| $q_1$ | a,B | - | - | $q_r$ |
| $q_2$ | 1 | 1 | R | $q_3$ |
| $q_2$ | 0,a,B | - | - | $q_r$ |
| $q_3$ | a | a | R | $q_8$ |
| $q_3$ | 0 | 0 | R | $q_6$ |
| $q_3$ | 1,B | - | - | $q_r$ |
| $q_4$ | 0 | 0 | R | $q_5$ |
| $q_4$ | 1,a,B | - | - | $q_r$ |
| $q_5$ | a | a | R | $q_9$ |
| $q_5$ | 0,1,B | - | - | $q_r$ |
| $q_6$ | 1 | 1 | R | $q_7$ |
| $q_6$ | 0,a,B | - | - | $q_r$ |
| $q_7$ | a | a | R | $q_{10}$ |
| $q_7$ | 0,1,B | - | - | $q_r$ |
| $q_8$ | B | B | - | $q_a$ |
| $q_8$ | 0,1,a | - | - | $q_r$ |
| $q_9$ | B | B | - | $q_a$ |
| $q_9$ | 0,1,a | - | - | $q_r$ |
| $q_{10}$ | B | B | - | $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$
| Step | State | Head reads | Action | Next |
|---|---|---|---|---|
| 1 | $q_0$ | a | write a, R | $q_1$ |
| 2 | $q_1$ | 0 | write 0, R | $q_2$ |
| 3 | $q_2$ | 1 | write 1, R | $q_3$ |
| 4 | $q_3$ | 0 | write 0, R | $q_6$ |
| 5 | $q_6$ | 1 | write 1, R | $q_7$ |
| 6 | $q_7$ | a | write a, R | $q_{10}$ |
| 7 | $q_{10}$ | B | write B | $q_a$ |
The machine halts in $q_a$ (final state), so the string a0101a is ACCEPTED. ✓
Turing Machine as a Computing Function
2080 · 5 marks
Design a Turing machine that computes a function f(n)=0. [5]
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]
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]
Introduction to Turing Machines
2078 · 5 marks
Define Turing Machine and explain its different variations. [5]
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]
Turing Machine with Multiple Tapes
2076 · 5 marks
Describe the Turing machines with multiple tape, multiple track and storage in state. [5]
Restricted Turing Machines
2080.1 · 5 marks
Describe how multi-stack TM is different from the semi-infinite tape TM? [5]
Make Unit 6 stick
Practice CSC262 with flashcards & quizzes