0% found this document useful (0 votes)
13 views7 pages

09 Exercises Solutions

The document contains exercise sheets for a course on Theoretical Computer Science and AI, focusing on Turing machines and their functionalities. It includes various exercises that require drawing transition diagrams for Turing machines, describing accepted languages, and constructing algorithms for specific functions. The exercises cover topics such as language acceptance, string comparison, and arithmetic operations using Turing machines.

Uploaded by

tahafteiti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views7 pages

09 Exercises Solutions

The document contains exercise sheets for a course on Theoretical Computer Science and AI, focusing on Turing machines and their functionalities. It includes various exercises that require drawing transition diagrams for Turing machines, describing accepted languages, and constructing algorithms for specific functions. The exercises cover topics such as language acceptance, string comparison, and arithmetic operations using Turing machines.

Uploaded by

tahafteiti
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Professur für Theoretische Informatik und KI OTH Regensburg/IM

Fundamentals of Computer Science 1


(Grundlagen der Informatik 1)
S. Scheele
Exercise Sheet 9

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}.

Solution: The Turing Machine is given below.

a/a, R b/b, R x/x, L for x ∈ Σ

∆/∆, R a/∆, R b/b, R ∆/∆, L b/∆, L


start q0 q1 q3 q4 q5 q6

b/b, R
∆/∆, R
b/b, R q2

∆/∆, R

S. Scheele 1 Winter Semester 2024


F UNDAMENTALS OF C OMPUTER S CIENCE 1 (GdI1) OTH Regensburg/IM

This machine could also be described using the compositional notation.

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}.

Solution: The machine is given below using the compositional notation.

x y z
start R S L LR¬ x S L LR¬y S L L∆

stop

S. Scheele 2 Winter Semester 2024


F UNDAMENTALS OF C OMPUTER S CIENCE 1 (GdI1) OTH Regensburg/IM

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

∆/∆, R 1/x, R 1/∆, L

∆/∆, R 1/x, R 1/1, R ∆/∆, L


q0 q1 q2 q3 q4

∆/∆, S
1/1, R

Describe the language L( M) ⊆ {1}∗ accepted by the Turing Machine M.

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∆∆∆ . . ..

S. Scheele 3 Winter Semester 2024


F UNDAMENTALS OF C OMPUTER S CIENCE 1 (GdI1) OTH Regensburg/IM

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.

Solution: The machine is given below using the compositional notation.

start R #R∗ R¬# #L∗ L#


ω ∈ { x, y, z} ω

∗ ¬ω

¬∆
R ¬# EN

EY

The machine above relies on a new building-block machine, Ex , given below.

¬∆


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).

Solution: The machine is given below using the compositional notation.

S. Scheele 4 Winter Semester 2024


F UNDAMENTALS OF C OMPUTER S CIENCE 1 (GdI1) OTH Regensburg/IM

{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.

S. Scheele 5 Winter Semester 2024


F UNDAMENTALS OF C OMPUTER S CIENCE 1 (GdI1) OTH Regensburg/IM

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.

Solution: Two Stacks


A machine with a two-way, read-only input head and two stacks is as powerful as a Turing machine.
Intuitively, the computation of a one-tape TM can be simulated with two stacks by storing the tape
contents to the left of the head on one stack and the tape contents to the right of the head on the
other stack. The motion of the head is simulated by popping a symbol off one stack and pushing it
onto the other. For example,

# 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:

• Stack 1 storing the string a (from bottom to top).


• Stack 2 storing the reverse of b, denoted b R (from bottom to top).

S. Scheele 6 Winter Semester 2024


F UNDAMENTALS OF C OMPUTER S CIENCE 1 (GdI1) OTH Regensburg/IM

Transitions: For each TM transition:

δ ( qi , ci ) = ( q j , c j , L )

The equivalent 2-PDA performs the following steps:

1. Pop ci from Stack 2 (the symbol under the head).


2. Push c j onto Stack 2 (writing the updated symbol).
3. Pop a symbol from Stack 1 (move left by transferring the top of Stack 1).

4. Push the popped symbol into Stack 2 (shift it to the right).


5. Update state from qi to q j .

Similarly, for the TM transition:


δ ( qi , ci ) = ( q j , c j , R )
The equivalent 2-PDA performs the following steps:

1. Pop ci from Stack 2 (the symbol under the head).


2. Push c j onto Stack 1 (writing the updated symbol and shifting right).
3. Update state from qi to q j .

Key Observations: The two stacks effectively simulate the infinite tape of a TM by dynamically
representing both directions of movement:

• Stack 1 simulates the tape content to the left of the head.

• Stack 2 simulates the tape content to the right of the head.

The PDA uses stack operations (push and pop) to replicate head movements, enabling it to simulate
a TM accurately.

[based on Solution by Sipser]

S. Scheele 7 Winter Semester 2024

You might also like