You are on page 1of 25

Theory of Computation (With Automata Theory)

TOPIC TITLE: Turing Machines


Specific Objectives:
At the end of the topic session, the students are expected to:
Cognitive:
1. Discuss the details of the pumping lemma for context-free
languages.
2. Use the pumping lemma to prove if a language is non-contextfree.
Affective:
1. Listen to others with respect.
2. Participate in class discussions actively.

MATERIALS/EQUIPMENT:
o
o

topic slides
OHP

TOPIC PREPARATION:
o
o
o
o

Turing Machines

Have the students review related topics that discussed in


previous courses.
Prepare the slides to be presented in class.
It is imperative for the instructor to incorporate various kinds of
teaching strategies while discussing the suggested topics.
Prepare additional examples on the topic to be presented.

*Property of STI
Page 1 of 25

Theory of Computation (With Automata Theory)

Turing Machines
Page 1 of 34

Turing Machines
Turing Machines
Introduction
As defined in previous lessons, a finite automaton (deterministic and
nondeterministic finite automata) is a mathematical model of a machine with a finite
number of states. It is used to model computers with a limited amount of memory,
or programs that process only a small amount of data.
The main disadvantage of a DFA and an NFA is its limited number of states. As
shown before, they cannot recognize languages such as the language L =
x x
{0 1 x > 0} since it would require the DFA or NFA to count and remember the
number of consecutive 0s and 1s it has received. This would require an infinite
number of states since the number of possible 0s and 1s are infinite. In other
words, it is very difficult to use a finite automaton to count things or events.

Turing Machines
Page 2 of 34

And, as presented recently, a pushdown automaton is similar to a finite automaton


except that it has access to a stack. The stack gives the PDA additional memory
beyond what is available in an NFA (as represented by the states). There can
potentially be an infinite number of items that can be stored in the stack. This
makes PDAs more powerful than NFAs because they can recognize certain nonregular languages. PDAs can then be used to model machines or computers with
unlimited memory.
The problem with PDAs is that access to the data in the stack is limited on a last-in,
first-out basis. Hence, although the memory of the PDA is virtually unlimited, it
does have restrictions.
R

Recall that a PDA can recognize the language L = {ww


w {0, 1}*}. This
language is composed of strings whose second half is equal to the reverse of the
first half. Recall the algorithm followed by the PDA that recognizes this language:
1.
2.
3.

4.

The PDA reads each symbol of the input string.


For each input symbol that it receives, the PDA pushes it onto the stack.
When the middle of the string is reached, the PDA starts popping off the
stack for each input symbol read, trying to determine if what is read at the
input and what is popped off the stack are the same symbols. Since the
stack follows the LIFO structure, popping the string from the stack causes
the symbols to come out in the reverse order.
If each symbol read from the input and popped out from the stack are
always the same, and the stack empties at the same time as the input is
finished, the string is accepted. Otherwise, it is rejected.

As can be seen, the stack was very useful in recognizing this language. However,
a PDA cannot recognize the language L = {ww w {0, 1}*} because it requires
that the symbols pushed onto the stack to come out in the order they were pushed,
not in the reverse order. This is an example of how the LIFO restriction prevents a
PDA from recognizing a language.
In general, finite automata and pushdown automata are limited to performing only
simple tasks, and therefore, they can only be used to represent simple machines.
Is there then a model (which has unlimited and unrestricted memory) that can truly
represent more complex machines such as general-purpose computers?

Turing Machines

*Property of STI
Page 2 of 25

Theory of Computation (With Automata Theory)

Turing Machines
Page 3 of 34

Turing Machines
A Turing machine (TM) is similar to a finite automaton and a pushdown
automaton except that it has unlimited and unrestricted memory.
It is similar to a PDA except that its storage device is a tape instead of a LIFO
stack. This tape can be viewed as being similar to the magnetic tape storage of
old computers, or even to the old cassette tapes.
A tape can be treated as an infinite, one-dimensional array of cells. Each cell can
store one symbol.
The TM has a read/write head that can access any cell on the tape. The head can
read or write one symbol at a time. To access a particular cell, the TM simply has
to move its head to that cell. Hence, there is no restriction on how data on the tape
can be accessed.

Turing Machines
Page 4 of 34

The figure below is a schematic representation of a Turing machine:


TM

Read/Write
Head

...

Tape

The TM can read and write/store symbols on the tape. The symbol will be used
here to represent an empty or blank cell.
The read/write head can move either to the left or the right to reach any cell. It can
read or write one symbol on each move.

Turing Machines

*Property of STI
Page 3 of 25

Theory of Computation (With Automata Theory)

Turing Machines
Page 5 of 34

Initially, the first few cells contain the symbols of the input string while the rest are
blank. Hence, it will be assumed that the input string that will be processed is
already stored on the tape before any computation begins.
When a PDA starts computing, it considers the current state it is in and the symbol
on the top of the stack to determine its next action (which state to go to and
whether to push something onto the stack).
For a TM, its next action is determined by the current state it is in and the symbol
on the tape being pointed to by the read/write head (the symbol right below the
head).
The next action of the TM is composed of

Turing Machines
Page 6 of 34

1.

moving to another state,

2.

replacing the symbol that is below the read/write head, and

3.

moving the read/write head to the left or to the right.

Case Study
Consider the language L1 = {w#w w {0, 1}*}. This language is composed of
strings where the first half is equal to the second half. The halves are separated by
the # symbol. Sample strings of this language are 110#110 and 11101#11101.
There is no NFA or PDA that can recognize this language. However, a TM can be
constructed to recognize it.
Here is an informal description of how the TM tries to determine if an input string
belongs to language L1:

Turing Machines
Page 7 of 34

1.

The TM will read the first symbol of the input string (the input string is
already on the tape). The TM then replaces this symbol with an x to
indicate that it has already processed it.

2.

The TM will then compare the symbol it has read in step 1 to the first
symbol of the string to the right of #. It therefore moves the head to the
first symbol to the right of #. Obviously, if the TM does not encounter a #,
the string is rejected.

3.

If the first symbol to the right of the # (the counterpart of the symbol read
in step 1) matches the symbol read earlier, replace this symbol with an x
to indicate that it has already been processed. If not, reject the string.

Note: Steps 1 to 3 simply compares an input symbol of the string on the left side of
# to its counterpart of the string on the right side of #.

Turing Machines

4.

Repeat steps 1 to 3 for each remaining symbol on the left side of #.

5.

When all symbols on the left side of # have been replaced with xs, check
if there are remaining symbols to the right of #. If there are, reject the
string. Otherwise, accept the string.

*Property of STI
Page 4 of 25

Theory of Computation (With Automata Theory)

Here is a simulation of the algorithm showing how the TM processes the string
110#110:
1.

The TM first moves its head to the first symbol of the input string to read
it.

2.

...

...

The TM then moves its head to the first symbol of the string on the right
side of # (the counterpart of the symbol it has read earlier).

4.

The TM remembers the symbol it has read (1) and replaces it with an x.

3.

...

Since the symbol read here matches the symbol read earlier, the TM
replaces this symbol with an x and continues processing.

...

If the two symbols did not match, the string would have been rejected,
and the processing stops or halts.
5.

The TM then moves its head to the second symbol of the input string.

...

The TM can easily locate this symbol since it is the first symbol to the
right of the first x it will encounter while moving to the left. This is the
reason why each symbol processed is replaced by an x.
6.

The TM remembers the symbol it has read (1) and replaces it with an x.

7.

...

The TM now moves its head to the second symbol of the string on the
right side of # (the counterpart of the symbol it has read earlier).

...

The TM can easily locate this symbol since it is the first symbol to the
right of the last x it will encounter while moving to the right.
8.

Turing Machines

Since the symbol read here matches the symbol read earlier, the TM
replaces this symbol with an x and continues processing.

*Property of STI
Page 5 of 25

Theory of Computation (With Automata Theory)

9.

...

The TM then moves its head to the third symbol of the input string(first
symbol to the right of the first x after # while moving to left).

...

10. The TM remembers the symbol it has read (0) and replaces it with an x.

...

11. The TM then moves its head to the third symbol of the string on the right
side of # (the counterpart of the symbol it has read earlier):

...

12. Since the symbol read here matches the symbol read earlier, the TM
replaces this symbol with an x.

...

Since all the symbols on the left side of # are xs, and there are no more
input symbols left, the string is accepted.
Turing Machines
Page 8 of 34

Formal Definition of Turing Machines


A Turing machine is a 7-tuple (Q, , , , q0, qaccept, qreject), where Q, , and
all finite sets, and
1.

Turing Machines

are

Q is the set of states,

2.

is the input alphabet,

3.

is the tape alphabet,

4.

is the transition function,

5.

q0

Q and is the start state,

6.

qaccept

7.

qreject

Q and is the accept state, and


Q and is the reject state, where qaccept

qreject.

*Property of STI
Page 6 of 25

Theory of Computation (With Automata Theory)

Turing Machines
Page 9 of 34

Turing Machines
Page 10 of 34

Some points regarding Turing machines:


does not include the blank symbol .

1.

The input alphabet

2.

The tape alphabet is composed of the symbols that can be stored on


the tape. These include the input alphabet , the blank symbol , and
some other symbols that may be stored in the tape. In the simulation
given earlier, the tape alphabet = {0, 1, x, }.

3.

Since the input string is stored in the first few cells of the tape, the
read/write head always starts at the leftmost cell (pointing to the first
symbol of the input string). The head cannot go to the left of the leftmost
cell.

4.

The first blank symbol encountered by the read/write (the blank symbol
to the right of the last symbol of the input string) indicates the end of the
input string.

5.

A TM has one accept state (qaccept) and one reject state (qreject).
Recall that if an NFA or PDA is on a final state at the end of the input
string, then the string is accepted. If the NFA or PDA ends up in any
other state, then the string is rejected. Also, it is possible for an NFA and
PDA machine to pass through a final state several times. This is
because final states in an NFA and PDA may have outgoing edges.
Once a TM lands in the accept state, it accepts the string being
processed and stops or halts the computation immediately. Accept
states do not have outgoing edges. In other words, once the TM enters
qaccept, it has nowhere else to go.
If the TM lands in the reject state, it rejects the string being processed,
and stops or halts the computation immediately. Like accept states,
reject states do not have outgoing edges.
In other words, the accept and reject states are halting states. They
cause the computation to halt or stop immediately.
If the TM ends up in any other state at the end of the input string, the TM
is said to be in a loop. Looping does not necessarily mean that the TM is
continuously repeating a series of actions. It could mean that the TM is in
a complex situation that never leads to a halting state.

Turing Machines

*Property of STI
Page 7 of 25

Theory of Computation (With Automata Theory)

Turing Machines
Page 11 of 34

In a TM, transitions are represented by


(qi, a) = (qj, b, R)
This equation indicates that if the current state is state qi and the symbol below the
read/write head is a, then the TM goes to state qj, writes the symbol b on the tape,
and moves the head to the right. This implies that the symbol a will be overwritten
(replaced) by the symbol b.
Naturally, transitions can also be
(qi, a) = (qj, b, L)
where the head moves to the left instead of right.

Take note that the transitions (qi, a) = (qj, a, R) and (qi, a) = (qj, a, L) imply that
the contents of the tape will not change.

Turing Machines
Page 12 of 34

Configuration of a Turing Machine


The configuration of a TM is defined by the current state it is in, the contents of
the tape, and the position of its read/write head.
It is written as
xqy
where q is the current state of the TM, x is the string on the left side of the current
head position, and y is the string on the right side of the current head position.
Note that the string y starts at the symbol being pointed to by the head.

Turing Machines
Page 13 of 34

Example:
TM

q3

...

Tape

Since the string at the left side of the head is 101 and the string on the right side
(starting at the head) is 0011, then the configuration of the TM at this point in time
is 101q30011.

Turing Machines

*Property of STI
Page 8 of 25

Theory of Computation (With Automata Theory)

Bear in mind that the configuration of the TM at the start of any computation is q0w
where q0 is the start state and w is the input string. This is because the read/write
head points to very first cell of the tape before any computation begins. Hence,
there is no string to the left of the head, and the string to its right is the entire input
string w.
Turing Machines
Page 14 of 34

Configuration C1 is said to yield configuration C2 if the TM can reach C2 from C1 in


one step. This is represented as
C1 C2
Assume that the current configuration is x0qi1y as shown below:
TM

qi

...

...

y
configuration = x0qi1y

If the transition function is (qi, 1) = (qj, 0, L), then x0qi1y yields xqj00y or
x0qi1y xqj00y
TM

qj

...

...

y
configuration = xqi00y

Similarly, if the current configuration is x0qi1y and the transition function is (qi, 1) =
(qj, 0, R), then
x0qi1y x00qjy

Turing Machines

*Property of STI
Page 9 of 25

Theory of Computation (With Automata Theory)

Turing Machines
Page 15 of 34

Sample Transitions in TM State Diagrams


Sample 1:

In this example, assume that the TM is currently at state qi. The transition label is
0 x, R. This means that if the symbol under the read/write head is a 0, then the
TM moves from state qi to state qj, writes the symbol x on the tape (effectively
replacing the symbol 0), and moves the head to the right.

Proof of Induction
Page 16 of 34

Sample 2:

In this example, assume that the TM is currently at state qi. The transition label is
0 L. This means that if the symbol under the read/write head is a 0, then the TM
moves from state qi to state qj, and moves the head to the left. No change is done
on the tape.
As mentioned earlier, the label 0 0, L will also accomplish the same thing.

Turing Machines

*Property of STI
Page 10 of 25

Theory of Computation (With Automata Theory)

Turing Machines
Page 17 of 34

Sample 3:

In this example, assume that the TM is currently at state qi. The transition label is
L. This means that if the symbol under the read/write head is a blank (),
then the TM moves from state qi to state qj, and moves the head to the left. No
change is done on the tape.
As before, the label , L will also accomplish the same thing.
Sample 4:

In this example, assume that the TM is currently at state qi. The transition label is
0, 1 x, L. This means that if the symbol under the read/write head is either a 0
or a 1, then the TM moves from state qi to state qj, writes the symbol x on the tape
(effectively replacing the symbol 0 or 1), and moves the head to the left.
Turing Machines
Page 18 of 34

Language of a Turing Machine


If a language is recognized by a Turing machine, then it is said to be Turingrecognizable.
A TM recognizes a language if it accepts all the strings of the language. Take note
that a TM does not accept a string if it ends up in the reject state qreject (thereby
rejecting the string), or it ends up in any other state other than qaccept or qreject (loop).
However, there are TMs that always end up in the reject state for all strings that do
not belong to the language of the TM. In other words, these TMs always end up in
a halting state for every input it receives. These Turing machines are called
deciders. Deciders accept all the strings of the language, and reject all strings not
in the language. They never loop. Languages that are recognized by deciders are
said to be decided by the TM.
If a language is decided by a Turing machine, then the language is Turingdecidable or simply decidable.

Turing Machines

*Property of STI
Page 11 of 25

Theory of Computation (With Automata Theory)

Turing Machines
Page 19 of 34

Going back to the case study presented earlier, the state diagram of the TM M1
that can decide the language L1 = {w#w w {0, 1}*} is given below:

The diagram shows that M1 has 9 states (q0, q1, q2, q3, q4, q5, q6, q7, and qaccept).
To simplify the diagram, the reject state qreject and all the other states that lead to it
from the 9 states listed were not shown. If there is a transition from any of the
9 states that is not listed in the state diagram, it will be assumed that M1 will
eventually go to the reject state.
For example, there is an outgoing edge from state q4 labeled x
R that leads
back to itself. This means that if the symbol below the read/write head is x, then
M1 goes back to state q4 and moves the head to the right. There is also another
outgoing edge labeled 1
x, L that leads to state q5. This means that if the
symbol below the read/write head is a 1, then M1 goes to state q5, replaces the
symbol 1 with an x, and moves the head to the left. Notice that there is no
transition edge for the situation where the symbol below the head is a 0. This
means that if the symbol below the head is a 0, M1 will go to a series of transitions
using several states (that are not listed) until it reaches the reject state qreject.

Turing Machines

*Property of STI
Page 12 of 25

Theory of Computation (With Automata Theory)

Turing Machines
Page 20 of 34

Simulating how M1 accepts the string 110#110, the following are the series of
configurations M1 enters:
Initially, M1 is at the start state q0 and the read/write head points to the first cell (the
leftmost cell) of the tape:
Current
State = q0

...

Configuration = q0110#110

From state q0, since the symbol below the head is 1, M1 goes to state q2, replaces
the symbol 1 with an x, and then moves the head to the right.
Current
State = q2

...

Configuration = xq210#110

From state q2, since the symbol below the head is 0, M1 goes back to state q2, and
then moves the head to the right. This is where the TM starts going to the
counterpart of the symbol just processed on the right side of #.
Current
State = q2

...

Configuration = x1q20#110

From state q2, since the symbol below the head is 0, M1 goes back to state q2, and
then moves the head to the right.
Current
State = q2

...

Configuration = x10q2#110

From state q2, since the symbol below the head is #, M1 goes to state q4, and then
moves the head to the right.
Current
State = q4

...

Configuration = x10#q4110

At this point, the TM has reached the counterpart of the symbol that was processed
earlier.

Turing Machines

*Property of STI
Page 13 of 25

Theory of Computation (With Automata Theory)

From state q4, since the symbol below the head is 1 (same as the first symbol
read), M1 goes to state q5, replaces the symbol 1 with an x, and then moves the
head to the left.
Current
State = q5

...

Configuration = x10q5#x10

From state q5, since the symbol below the head is #, M1 goes to state q6, and
moves the head to the left. This is where the TM starts going back to the left side
of # to process the next symbol.
Current
State = q6

...

Configuration = x1q60#x10

From state q6, since the symbol below the head is 0, M1 goes back to state q6, and
moves the head to the left.
Current
State = q6

...

Configuration = xq610#x10

From state q6, since the symbol below the head is 1, M1 goes back to state q6, and
moves the head to the left.
Current
State = q6

...

Configuration = q6x10#x10

From state q6, since the symbol below the head is x, M1 goes to state q0, and
moves the head to the right. Since it has encountered an x, this means the next
symbol to be processed is the symbol to the right of x.
Current
State = q0

...

Configuration = xq010#x10

From state q0, since the symbol below the head is 1, M1 goes to state q2, replaces
the symbol 1 with an x, and then moves the head to the right.

Turing Machines

*Property of STI
Page 14 of 25

Theory of Computation (With Automata Theory)

Current
State = q2

...

Configuration = xxq20#x10

From state q2, since the symbol below the head is 0, M1 goes back to state q2, and
then moves the head to the right. This is where the TM starts going to the
counterpart of the symbol recently processed on the right side of #.
Current
State = q2

...

Configuration = xx0q2#x10

From state q2, since the symbol below the head is #, M1 goes to state q4, and
moves the head to the right.
Current
State = q4

...

Configuration = xx0#q4x10

From state q4, since the symbol below the head is x, M1 goes back to state q4, and
moves the head to the right.
Current
State = q4

...

Configuration = xx0#xq410

From state q4, since the symbol below the head is 1, M1 goes to state q5, replaces
the symbol 1 with an x, and then moves the head to the left.
Current
State = q5

...

Configuration = xx0#q5xx0

From state q5, since the symbol below the head is x, M1 goes back to state q5, and
moves the head to the left. This is where the TM starts going back to the left side
of # to process the next symbol.

Turing Machines

*Property of STI
Page 15 of 25

Theory of Computation (With Automata Theory)

Current
State = q5

...

Configuration = xx0q5#xx0

From state q5, since the symbol below the head is #, M1 goes to state q6, and
moves the head to the left.
Current
State = q6

...

Configuration = xxq60#xx0

From state q6, since the symbol below the head is 0, M1 goes back to state q6, and
moves the head to the left.
Current
State = q6

...

Configuration = xq6x0#xx0

From state q6, since the symbol below the head is x, M1 goes to state q0, and
moves the head to the right. Since it has encountered an x, this means the next
symbol to be processed is the symbol to the right of this x.
Current
State = q0

...

Configuration = xxq00#xx0

From state q0, since the symbol below the head is 0, M1 goes to state q1, replaces
the symbol 0 with an x, and then moves the head to the right.
Current
State = q1

...

Configuration = xxxq1#xx0

From state q1, since the symbol below the head is #, M1 goes to state q3, and
moves the head to the right. This is where the TM starts going to the counterpart
of the symbol recently processed on the right side of #.

Turing Machines

*Property of STI
Page 16 of 25

Theory of Computation (With Automata Theory)

Current
State = q3

...

Configuration = xxx#q3xx0

From state q3, since the symbol below the head is x, M1 goes back to state q3, and
moves the head to the right.
Current
State = q3

...

Configuration = xxx#xq3x0

From state q3, since the symbol below the head is x, M1 goes back to state q3, and
moves the head to the right.
Current
State = q3

...

Configuration = xxx#xxq30

From state q3, since the symbol below the head is 0, M1 goes to state q5, replaces
the symbol 0 with an x, and then moves the head to the left.
Current
State = q5

...

Configuration = xxx#xq5xx

From state q5, since the symbol below the head is x, M1 goes back to state q5, and
moves the head to the left. This is where the TM starts going back to the left side
of # to process the next symbol.
Current
State = q5

...

Configuration = xxx#q5xxx

From state q5, since the symbol below the head is x, M1 goes back to state q5, and
moves the head to the left.

Turing Machines

*Property of STI
Page 17 of 25

Theory of Computation (With Automata Theory)

Current
State = q5

...

Configuration = xxxq5#xxx

From state q5, since the symbol below the head is #, M1 goes to state q6, and
moves the head to the left.
Current
State = q6

...

Configuration = xxq6x#xxx

From state q6, since the symbol below the head is x, M1 goes to state q0, and
moves the head to the right. Since it has encountered an x, this means the next
symbol to be processed is to the right of this x.
Current
State = q0

...

Configuration = xxxq0#xxx

From state q0, since the symbol below the head is #, M1 goes to state q7, and
moves the head to the right. Since it has encountered the # to the right of an x,
this means that all the symbols on the left side of # have been processed. The
next step now is to determine if there are any unprocessed symbols to the right of
#.
Current
State = q7

...

Configuration = xxx#q7xxx

From state q7, since the symbol below the head is x, M1 goes back to state q7, and
moves the head to the right.
Current
State = q7

...

Configuration = xxx#xq7xx

From state q7, since the symbol below the head is x, M1 goes back to state q7, and
moves the head to the right.

Turing Machines

*Property of STI
Page 18 of 25

Theory of Computation (With Automata Theory)

Current
State = q7

...

Configuration = xxx#xxq7x

From state q7, since the symbol below the head is x, M1 goes back to state q7, and
moves the head to the right.
Current
State = q7

...

Configuration = xxx#xxxq7

Since the symbol below the read/write head is the blank symbol , this means that
the TM did not encounter any unprocessed symbols on the right side of #. This
implies that the string on the left side and the string on the right side of # are equal.
Hence, from state q7, M1 goes to the accept state qaccept. The string 110#110 is
therefore accepted.
Turing Machines
Page 21 of 26

Example 2
2n

Consider the language L2 = {0


n 0}. This language is composed of 0s whose
total number is a power of two (1, 2, 4, 8, 16, etc.).
The first question is how does one determine if a number is a power of two. If any
power of 2 (except 1) is divided by two repeatedly, the division will end up with a 1.
For example:
16 2 = 8
8 2=4
4 2=2
2 2=1
If this is performed on any number that is not a power of two, the division will end
up with an odd number greater than 1.
Based on this, a high-level description of the algorithm that will be followed by the
TM M2 that will decide the language L2 is given below:

Turing Machines

1.

Move the read/write from left to right across the tape (starting at the
leftmost cell and up to the first blank symbol). As this is done, replace
every other 0 that is encountered with an x. This effectively divides the
number of 0s in the input string by 2.

2.

If there is only one remaining 0 after step 1, accept the string and the TM
halts.

3.

If the number of remaining 0s is odd and greater than 1, reject the string
and the TM halts. This is identified by encountering a blank after a new 0
(i.e., other than the first 0 initially replaced by a blank) is found.

4.

If the number of remaining 0s is an even number, move the head back to


the first cell (the leftmost cell) and repeat step 1. This is identified by
encountering a blank while looking for a new 0.

*Property of STI
Page 19 of 25

Theory of Computation (With Automata Theory)

Applying this algorithm to the input string 00000000 (8 0s):


Assume that the input string is already stored in the tape as shown
below:

...

Before applying step 1, the first 0 will be replaced by the blank symbol .
The purpose of this is for the TM to know where the first cell is when
step 4 is performed. The tape contents will then be

...

Take note that the TM will include this blank symbol in counting the
number of 0s.
Apply step 1 and move the head across the tape, replacing every other 0
encountered with an x. The result is shown below:

...

At this point, the number of 0s remaining is 4 (including the first blank


symbol). The TM then moves the head back to the first cell by moving to
the left until it encounters the first blank symbol, and repeats step 1. This
is the reason why the first 0 was replaced by a so that the TM will
know if it has reached the first cell.

...

Apply step 1 again and move the head across the tape, replacing every
other 0 encountered with an x. The result is shown below (new xs are
larger for easy visual identification):

...

...

One final application of step 1 results in

Since the number of remaining 0s is 1 (the first 0), the TM accepts the
string.

Turing Machines

*Property of STI
Page 20 of 25

Theory of Computation (With Automata Theory)

Turing Machines
Page 22 of 26

Turing Machines
Page 23 of 26

The state diagram of the TM M2 that can decide the language L2 = {0


given below:

2n

n 0} is

Simulating how M2 accepts the string 00000000, the following are the series of
configurations M2 enters:
The initial configuration will be q000000000. The following are the configurations
that M2 enters as it makes the first pass, replacing every other 0 it encounters with
an x (as mentioned earlier, the first 0 will be replaced by a ):
q10000000
xq2000000
x0q300000
x0xq20000
x0x0q3000
x0x0xq200
x0x0x0q30
x0x0x0xq2
At this point, M2 had reached the end of the input string. It will now go back to the
start of the tape for the second pass. The following will be the configurations M2
enters as it goes back to the first cell:
x0x0x0q4x
x0x0xq40x
x0x0q4x0x
x0xq40x0x
x0q4x0x0x
xq40x0x0x
q4x0x0x0x
q4x0x0x0x
M2 will now start the second pass. The following will be the configurations:
q1x0x0x0x
xq10x0x0x

Turing Machines

*Property of STI
Page 21 of 25

Theory of Computation (With Automata Theory)

xxq2x0x0x
xxxq20x0x
xxx0q3x0x
xxx0xq30x
xxx0xxq2x
xxx0xxxq2
At this point, M2 had reached the end of the input string for the second time. It will
now go back to the start of the tape for the third pass. The following will be the
configurations M2 enters as it goes back to the first cell:
xxx0xxq4x
xxx0xq4xx
xxx0q4xxx
xxxq40xxx
xxq4x0xxx
xq4xx0xxx
q4xxx0xxx
q4xxx0xxx
Turing Machines
Page 24 of 26

M2 will now start the third pass. The following will be the configurations:
q1xxx0xxx
xq1xx0xxx
xxq1x0xxx
xxxq10xxx
xxxxq2xxx
xxxxxq2xx
xxxxxxq2x
xxxxxxxq2
Since M2 encountered only a single 0 in its third pass, this means that the number
of 0s in the input string is a power of 2. M2 will now make the state transitions
necessary in order to reach the accept state. The following will be the
configurations:
xxxxxxq4x
xxxxxq4xx
xxxxq4xxx
xxxq4xxxx
xxq4xxxxx
xq4xxxxxx
q4xxxxxxx
q4xxxxxxx
q1xxxxxxx
xq1xxxxxx
xxq1xxxxx
xxxq1xxxx
xxxxq1xxx
xxxxxq1xx
xxxxxxq1x
xxxxxxxq1
From state q1, since the symbol below the head is the blank symbol, M2 will now go
the accept state qaccept.

Turing Machines

*Property of STI
Page 22 of 25

Theory of Computation (With Automata Theory)

Turing Machines
Page 25 of 26

Example 3
i j k

Consider the language L3 = {a b c i j = k and i, j, k 0}. This language is


composed of consecutive as, followed by consecutive bs, and then followed by
consecutive cs. The number of cs is equal to the number of as times the number
of bs.
In order for a TM to determine if a string belongs to this language, it must be able
to perform multiplication. The multiplication algorithm that may be used by this TM
can be best explained by the following example:
Assume that the string to be processed is aabbbcccccc. There are 2 as, 3
bs, and 6 cs. This string belongs to language L3 because the number of cs
(6) is equal to the number of as (2) times the number of bs (3).
The algorithm can be stated as:
For each a, replace one c with an x for every b. If there are no cs
remaining after processign all the bs, then accept the string.
Since there are two as in the given string, the TM should replaced 3 cs with
xs (since there are 3 bs) two times. Since this will cause all cs to be
replaced, the string is accepted. This is illustrated by the following:
Initially, the tape contains

...

The TM will now start processing the first a. It will replace this a with a y (or
any other symbol) so that it will remember which of the as have been
processed at any point in time. This is shown below:

...

The TM will now start replacing a c with an x for every b. It will replace the
first b with a z to remember which of the bs have been processed. This is
shown below:

...

For this first b, one c will be replaced with an x as shown below:

...

The TM will now go to the second b. It will also replace this b with a z. This is
shown below:

Turing Machines

*Property of STI
Page 23 of 25

Theory of Computation (With Automata Theory)

...

For this second b, another c will be replaced with an x as shown below:

...

The TM will now go to the third b. It will replace this b with a z. This is shown
below:

...

For this third b, another c will be replaced with an x as shown below:

...

Since there are no more bs, the TM will repeat everything for the second a.
As with the first a, it will replace this a with a y. This is shown below:

...

Notice that all the bs were replaced by zs earlier. Since the bs are needed
again, they will be restored by the TM (replace all the zs with bs).

...

The TM will again start replacing a c with an x for every b. It will again replace
the first b with a z to remember which of the bs have been processed. This is
shown below:

...

For this first b, one c will be replaced with an x as shown below:

...

The TM will now go to the second b. It will also replace this b with a z. This is
shown below:

Turing Machines

*Property of STI
Page 24 of 25

Theory of Computation (With Automata Theory)

...

For this second b, another c will be replaced with an x as shown below:

...

The TM will now go to the third b. It will replace this b with a z as shown
below:

...

For this third b, another c will be replaced with an x as shown below:

...

Since there are no more as and bs to be processed (found by finding a y


when the head moves back after replacing the bs), and all the cs were
replaced by xs (found by finding a blank after the last x), the string is
accepted.
Turing Machines
Page 26 of 26

Here is the high-level description of the algorithm that will be used by the TM for
language L3:
1.

Scan the tape from left to right to determine if the input string is
composed of consecutives as, followed by consecutive bs, and then
followed by consecutive cs. If not, reject the string. (This is actually
optional since you can check this along the way.)

2.

Move the read/write head back to the first cell (the leftmost cell) of the
tape.

3.

Replace an a with a y. Scan to the right until a b is reached. Sweep


between bs and cs, replacing each c with an x until all bs are processed
(marked by replacing a b with a z). If there are not enough cs, reject the
string.

4.

Restore the bs (previously replaced by zs). If there are as remaining,


then repeat step 3.

5.

After all the as are replaced by ys, determine if all the cs were replaced
by xs. If there are no remaining cs, then accept the string. Else, reject
the string.

The state diagram for this TM will not be shown anymore because of its
complexity. In fact, most exercises on Turing machines will only require a highlevel description on how the TM will perform its computations.

[Turing Machines, Pages 126 of 26]

Turing Machines

*Property of STI
Page 25 of 25