09 Exercises Solutions
09 Exercises Solutions
Turing Machines
Notations: In some of the exercises below, we will use the compositional machine notation
introduced in the lectures. This notation relies on a small number of primitive or derived machines,
some of which we recall below (a ∈ Γ is an arbitrary tape symbol):
• The machine R a searches for the first occurrence of the letter a that is strictly to the right of
the tape head. If a is never found, the machine does not terminate. If a is found, the machine
halts with the tape head positioned on its cell.
• The machine L a is similar to R a , but searches leftward. It terminates with an error if a is not
found.
• The machine R¬a searches rightward for the occurrence of a letter which is not a. If such a
letter is never found, the machine does not terminate. If such a letter is found, the machine
halts with the tape head positioned on its cell.
• Similarly for L¬a . It terminates with an error if the portion of tape extending from the left of
the tape head contains only as.
• The machine SR shifts the word extending from the left of the tape head to the next ∆, if there
is one, by one cell to the right. For example, [∆xxyyx∆] becomes [∆∆xxyx∆].
• The machine S L shifts the word extending from the right of the tape head to the next ∆, if
there is one, by one cell to the left. For example, [∆xxyyx∆] becomes [∆xxyx∆].
Exercise 9.1. Draw a transition diagram for a Turing Machine over the input alphabet Σ = { a, b},
accepting the language { ai b j | 0 ≤ i < j}.
b/b, R
∆/∆, R
b/b, R q2
∆/∆, R
a b ∆
start R ∆R¬a R¬b L∆L∆
R¬b
stop
Exercise 9.2. Draw a transition diagram for a Turing Machine over the input alphabet Σ = { x, y, z}
accepting the language { x n yn zn | n ≥ 0}.
x y z
start R S L LR¬ x S L LR¬y S L L∆
stop
Exercise 9.3. Consider the following Turing Machine M with tape alphabet Γ = {1, x, ∆}:
x/1, L 1/1, L
∆/∆, L
q7 q6 q5
x/x, R
∆/∆, S
1/1, R
n
Solution: We have L( M ) = {12 | n ≥ 0}.
Exercise 9.4. Describe the language L( M) ⊆ Γ∗ accepted by the following Turing Machine M, with
tape alphabet Γ = {1, ∆}:
1/∆, L
∆/∆, R ∆/∆, L
q0 q1 q2 q3
1/∆, L
1/1, R
∆/∆, L
∆/∆, S
Solution: We have L( M ) = {1n | n is even} since the transition from q3 to h tries to move the tape
head outside of the tape, and thus leads to an error.
Exercise 9.5. Consider the following algorithm for comparing two equal length strings in Σ∗ = { x, y, z}∗ .
Write the two strings on the TM’s tape separated by a ∗. E.g.:
∆xyzx ∗ xyzx∆∆∆ . . .
Shuttle back and forth between the two strings, first comparing characters in the first position, char-
acters in the second position, and so on until either the end of the string is reached or a difference is
found. As characters are considered in each copy of the string replace them with #’s to record that they
have been examined.
If the strings match erase the string, write a Y, and terminate in the configuration ∆Y∆∆∆ . . ..
If the strings differ erase the string, write a N, and terminate in the configuration ∆N∆∆∆ . . ..
If the input tape does not contain a valid input (i.e., does not contain ∗), your machine should crash.
Construct a Turing Machine over tape alphabet Γ = { x, y, z, ∆, ∗, #, Y, N } that implements this algo-
rithm. You may use any of the building-block Turing machines, including new ones of your own design
if needed.
∗ ¬ω
¬∆
R ¬# EN
EY
¬∆
∆
start LR∆ L RxL
Exercise 9.6. Using any of the building block machines, or otherwise, draw a Turing Machine that com-
putes the function f ∈ N → N given as f ( x ) = x + 2, using binary coding. The tape alphabet is
Γ = {0, 1, ∆}. The machine when started in the input tape configuration ∆bn ∆∆ · · · , where bn is the
binary coding of a number n ∈ N, halts with the output tape ∆bn+2 ∆∆ · · · .
For instance, ∆01010∆∆ · · · (code of the number 10) results in ∆01100∆∆ · · · (which codes 12), and
∆111∆∆ · · · (which codes 7) results in ∆1001∆∆ · · · (which is 9).
{0, 1} ∆
start R∆ L L R∆ SR L∆ R 1 L
1L∆
Exercise 9.7. Design a Turing machine that computes the subtraction function
m − n if m ≥ n
monus(m, n) =
0 otherwise
over natural numbers N. It is called the monus function, or proper subtraction. Sometimes the monus
.
operation is written m − n.
Numbers n, m ∈ N are to be repesented in the “neolithic” fashion as strings of m and n successive 0’s,
respectively, and separated by a single 1. For instance, 5 is coded as 00000 and 12 as 000000000000, and
the input pair (5, 12) as 000001000000000000, which we can abbreviate as 05 1012 .
Design the Turing machine M with tape alphabet Γ = {0, 1, ∆} so that if started with an input tape
∆0m 10n ∆∆∆ · · · then it terminates with the tape configuration ∆0monus(m,n) ∆∆∆ · · · .
Solution:
The machine M repeatedly finds the last 0 of the second argument, i.e. the rightmost 0 overall, then
moves left to find the first 1. After finding a 1, it continues left, until it comes to the rightmost 0
of the first argument, which it replaces by a 1. M then returns right, seeking the rightmost 0 of
the second argument, which it finds by searching right for a ∆ and moving left by one cell. The
iteration terminates in two ways:
1. Exit 1 Searching left for a 0 in the first argument, which it wishes to replace by 1, it reaches
a ∆ instead. This means the first argument is m was smaller than the second n, in which case
M resets all 1’s and remaining 0’s from the second argument by ∆ and returns to initial tape
position. Then the tape is ∆∆∆ · · · , i.e. the result is 0.
2. Exit 2 Searching right for the last 0 in the second argument may fail, in which case all 0’s of
the second argument have already been subtracted from the first argument. M then sweeps
left replacing all 1’s by ∆’s and moves to its initial tape position.
1
¬1
∆L L∆ stop
1
2
it
Ex
start R∆ L ∆
0
∆ ∆
∆L1 L¬1 R L
Exit 1
1 0 ¬∆ ¬∆
1 ∆
Exercise 9.8. Show that it is possible to simulate a Turing Machine using a push-down automaton with
2 Stacks.
# a b a a a b a b b b b b a a a b ...
is simulated by
# a b a a a b a b b b b b a a a b #
stack 1 stack 2
A Turing Machine (TM) can be simulated using a push-down automaton (PDA) with 2 stacks as
follows. We conceptualize the infinite tape of a TM as split into two separate stacks:
• Stack 1 stores the symbols to the left of the tape head. The bottom of the stack represents the
leftmost symbol on the tape.
• Stack 2 stores the symbols to the right of the tape head. The bottom of the stack represents
the rightmost symbol on the tape.
Given a TM configuration aqi b (where qi is the current state, a represents the content to the left, and
b represents the content to the right), the equivalent 2-PDA configuration has:
δ ( qi , ci ) = ( q j , c j , L )
Key Observations: The two stacks effectively simulate the infinite tape of a TM by dynamically
representing both directions of movement:
The PDA uses stack operations (push and pop) to replicate head movements, enabling it to simulate
a TM accurately.