You are on page 1of 40

Theory of Computation

Lecture #3
Sarmad Abbasi
Virtual University

Sarmad Abbasi (Virtual University)

Theory of Computation

1 / 40

Lecture 3: Overview

Quickly recall the definition of a Turing machine.


Look at some more examples of Turing machines.
Define the concept of a configuration.
Define what the language accepted by a TM.

Sarmad Abbasi (Virtual University)

Theory of Computation

2 / 40

Turing Machines
Let us once again make sure we understand what a Turing machine is?
Formally a Turing machine M is a 7-tuple, (Q, , , , q0 , qa , qr ), where
Q, , are all finite sets.
1

Q is the set of states.

is the input alphabet not containing the special blank symbol .

is the tape or work alphabet. and  .

q0 is the start state.

qa is the accept state.

qr is the reject state.

: Q Q {L, R} is the transition function.

Sarmad Abbasi (Virtual University)

Theory of Computation

3 / 40

Example: Turing Machines

The transition function is given by the following table.


state symbol next state replace with move
q0
0
q0
0
R
q0
1
q1
1
R
q0

qr

R
q1
0
q0
0
R
q1
1
q1
1
R
q1

qa

L
One again we have not specified what the machine does when it
reaches qa or qr . Let us see what this machine does since the
machine halts when it reaches the accept or the reject state.

Sarmad Abbasi (Virtual University)

Theory of Computation

4 / 40

Turing Machines
Let us intuitively see what each state is doing. Let us look at q0 . When
the machine is in q0 represents the fact that the last scanned symbol
was not a 1. Note that
(q0 , 0) = (q0 , 0, R)
so if the machine reads a 0 it simply stays in the state q0 and moves
right (without changing the input). However, when the machine sees a
1 it changes its state to q1 as
(q0 , 1) = (q1 , 1, R).
When the machine is in the state q1 that represents the fact that the
last scanned symbol was a 1. Thus if the machine is in the state q1
and sees a 1 it stays in q1 and if it sees a 0 it goes back to q0 .
(q1 , 0) = (q0 , 0, R) and (q1 , 1) = (q1 , 1, R)
Sarmad Abbasi (Virtual University)

Theory of Computation

5 / 40

Turing Machines

Lastly, we look at the transitions


(q0 , ) = (qr , , R)
which means if the input has ended and the last symbol was not 1 then
reject the input. Whereas,
(q1 , ) = (qa , , R)
if the input has ended and the last symbol was a 1 accept the input.

Sarmad Abbasi (Virtual University)

Theory of Computation

6 / 40

Turing machine: State Diagram

We can represent a Turing machine with a state diagram.

Sarmad Abbasi (Virtual University)

Theory of Computation

7 / 40

State Diagrams

To make a state diagram follow the following rules:


1

Each state is represented by a node in the state diagram.

Clearly, mark the initial state, the accept and the reject state. In
this course we will always mark the accept state with qa and reject
state with qr .

For each state (other than the accept and reject states) there
should be as many arrows coming out of the state as there are
symbols in the work alphabet.

Each arrow should be labeled by a replace symbol and a move.

Sarmad Abbasi (Virtual University)

Theory of Computation

8 / 40

Configurations

As a TM computes changes occur in three things:


1

Its state.

The tape contents.

It head position.

A setting of all these things is called a configuration. Hence a


configuration tells us what state the machine is in, what is currently
written on the tape and where the tape head exactly is.

Sarmad Abbasi (Virtual University)

Theory of Computation

9 / 40

Configurations
We will represent a configuration in a very special way. Suppose is
the tape alphabet. Let u, v and q be a state of the Turing
machine. Then the configuration
uqv
denotes
1

The current state is q.

The tape contents are uv .

The tape head is reading the first symbol of v .

It is assumed that the tape contents are all blanks after the last symbol
of v .
Let us look at some examples:

Sarmad Abbasi (Virtual University)

Theory of Computation

10 / 40

Configurations
Let us see the following picture.
The machine is in state q7 . The tape contents are 101101111 and the
tape head is currently reading the fifth symbol with is a 0.

So in this case, the first four symbols constitute of u and the last five
symbols constitute of v . So, this configuration will be written as
1011q7 01111

Sarmad Abbasi (Virtual University)

Theory of Computation

11 / 40

Configurations

Now, let us go the other way. Suppose I tell you that a machine is in
configuration
0abbaq3 bba01
Can you draw a picture? The tape contents are easily recovered by
ignoring the state in the configuration. So, the tape contents are
0abbabba01 and the machine is in state q3 . Lastly, it is reading the 6th
symbol on the tape.

Sarmad Abbasi (Virtual University)

Theory of Computation

12 / 40

Configurations

Sarmad Abbasi (Virtual University)

Theory of Computation

13 / 40

Configurations

Let us do one more example:


Suppose a TM is in the configuration 01q2 . What does this mean? In
this case, v = . So the machine is reading a blank symbol. The
picture is given here:

Sarmad Abbasi (Virtual University)

Theory of Computation

14 / 40

The Yields Relation

Now, we would like to formalize the way a TM computes or takes a


single step. This is done using the yields relation. Suppose that a
machine is in a configuration C1 . We say that the configuration C1
yields another configuration C2 if the machine can move legally from
C1 to C2 in a single step. Let us be a bit more formal.

Sarmad Abbasi (Virtual University)

Theory of Computation

15 / 40

The Yields Relation


Suppose a, b and u, v and qi , qj Q.
We say that
uaqi bv yields uqj acv
if (qi , b) = (qj , c, L).

Sarmad Abbasi (Virtual University)

Theory of Computation

16 / 40

The Yields Relation


This only handles the left moves, for the right moves we define the
yields relation as follows: We say that
uaqi bv yields uacqj v
if (qi , b) = (qj , c, R).

Sarmad Abbasi (Virtual University)

Theory of Computation

17 / 40

The Yields Relation


There are some special cases left to discuss for the yields relation.
What if the head is at one of the ends of the the configuration.
Let us discuss what if the head is at the left end.
qi bv yields qj cv
if (qi , b) = (qj , c, L).
and
qi bv yields cqj v
if (qi , b) = (qj , c, R).
In this case, we are agreeing that if the machine is at the left most cell
it will not move its head further to the left even if the delta function
requires us to do so.

Sarmad Abbasi (Virtual University)

Theory of Computation

18 / 40

The Yields Relation

What if the head is reading a blank?


uaqi yields uqj ac
if (qi , ) = (qj , c, L).
and
uaqi yields uacqj
if (qi , b) = (qj , c, R).

Sarmad Abbasi (Virtual University)

Theory of Computation

19 / 40

The Yields Relation


Have we discussed all possibilities?
One annoying one is left.
Consider a machine in a configuration
qi
Is this a configuration. Yes, the machine is in state qi and the tape is
completely blank and the machine is reading the left most symbol.
qi yields qj c
if (qi , ) = (qj , c, L).
and
qi yields cqj
if (qi , ) = (qj , c, R).
Sarmad Abbasi (Virtual University)

Theory of Computation

20 / 40

The Yields Relation

Summary:
uaqi bv yields uqj acv
if (qi , b) = (qj , c, L).
and
uaqi bv yields uacqj v
if (qi , b) = (qj , c, R).

Sarmad Abbasi (Virtual University)

Theory of Computation

21 / 40

The Yields Relation


Let us look at our previous TM and its diagram and see what the yields
relation gives us if we start the machine with input 1001.
Let us first look at the state diagram of this TM again.

The start configuration is q0 1001


and if we apply the yields operation repeatedly we get
1q1 001, 10q0 01, 100q0 1, 1001q1 , 1001qa .

Sarmad Abbasi (Virtual University)

Theory of Computation

22 / 40

The Yields Relation

For a TM M, if C1 yields C2 we sometime write this as:


C1 `M C2

Sarmad Abbasi (Virtual University)

Theory of Computation

23 / 40

Starting, accepting and rejection configurations

The start configuration of M on input w is q0 w where q0 is the


start state. Notice that the head is reading the left most symbol.

A configuration C is accepting if the machine is in the accept state.

A configuration C is rejecting if the machine is in the reject state.

Accepting and rejecting configurations are called halting


configurations.

Sarmad Abbasi (Virtual University)

Theory of Computation

24 / 40

Starting, accepting and rejection configurations

Suppose M is a TM and w . A TM accepts w if there exist


configurations C1 , C2 , . . . , Ck such that
1

C1 is the start configuration of M on input w; that is,


C1 = q0 w
.

each Ci `M Ci+1 .

Ck is an accepting configuration.

Sarmad Abbasi (Virtual University)

Theory of Computation

25 / 40

Language accepted by a TM

The collection of all strings in that are accepted by M is the


language of M and is denoted by L(M).
L(M) = {w : M accepts w}.

Sarmad Abbasi (Virtual University)

Theory of Computation

26 / 40

Language accepted by a TM

So, if we go to our previous example:

Sarmad Abbasi (Virtual University)

Theory of Computation

27 / 40

Language accepted by a TM

Sarmad Abbasi (Virtual University)

Theory of Computation

28 / 40

Language accepted by a TM

Then for this TM M


L(M) = {x : x ends with a 0}.

Sarmad Abbasi (Virtual University)

Theory of Computation

29 / 40

Language accepted by a TM
Does a TM only accept or reject inputs? No. Lets consider M 0 :

Hence if a string ends with a 0 the machine keeps computing forever.


q0 00 `M 0 0q0 0 `M 0 00q0 `M 0 `M 0 00q0 `M 0 00q0

Sarmad Abbasi (Virtual University)

Theory of Computation

30 / 40

Language accepted by a TM

What is the language accepted by M 0 ?


By definition it is the set of all strings accepted by M 0 . Hence,
L(M 0 ) = {x {0, 1} : x ends with a 0}.

Sarmad Abbasi (Virtual University)

Theory of Computation

31 / 40

Language accepted by a TM

We have
L(M) = {x{0, 1} : x ends with a 0}.
L(M 0 ) = {x{0, 1} : x ends with a 0}.
Both accept the same language. However, the first machine M will
reject all inputs which do not end with a 0. Whereas, M 0 will go into a
infinite loop on all such inputs.
We prefer M to M 0 .

Sarmad Abbasi (Virtual University)

Theory of Computation

32 / 40

Language accepted by a TM

We say that a Turing machine is a decider if it halts on all inputs in .


Hence M is a decider and M 0 is not a decider.

Sarmad Abbasi (Virtual University)

Theory of Computation

33 / 40

Designing TMs

The most usual question in computer science can be posed as follows:


Given a language L0 design a decider, M, such that
L(M) = L0 .
L0 is the given problem and M is the algorithm that solves or decides
the problem. Let us look at an example of this.

Sarmad Abbasi (Virtual University)

Theory of Computation

34 / 40

Designing TMs
Let = {0}.
n

L2 = {02 : n 0}.
We want to design a TM M2 such that
L(M2 ) = L2 .
Hence we want a TM M2 that accepts all the inputs in L2 .
In order to solve this problem we must first understand the language
L2 .
L2 = {0, 00, 0000, 00000000, 0000000000000000, . . . , }

Sarmad Abbasi (Virtual University)

Theory of Computation

35 / 40

Designing TMs
To design a TM we must have a plan in mind. How will we solve this
problem? Note that a TM can only have finite number of states and
therefore it cannot remember much. It does not even know how to
count (unless you teach it).
If I have some number of 0s and I cross each alternate one I will cut
the number of 0s into half.
Informal description of the M2 .
1

Sweep from the left to the right crossing off each other 0.

If there was a single 0 on the tape, accept.

If there were more then one 0s and the number was odd, reject.

Return the head to the left end and repeat.

Sarmad Abbasi (Virtual University)

Theory of Computation

36 / 40

Designing TMs

Why would this work.


If the number of 0s is 16, it will be cut into 8 and then 4 and then 2 and
then 1 and then we will accept.
On the other hand if the number of 0s is say 20, it will be cut to 10 then
5 and then we will reject.

Sarmad Abbasi (Virtual University)

Theory of Computation

37 / 40

Designing TMs

Let us look at the formal description of this Turing Machine. The


machine M2 = (Q, , , , q1 , qa , qr ) where,
Q = {q1 , q2 , q3 , q4 , q5 , qa , qr ).
= 0.
= {0, x, }.
q1 is the start state, qa is the accept state and qr is the reject state.
is described by the following state diagram.

Sarmad Abbasi (Virtual University)

Theory of Computation

38 / 40

Designing TMs

Sarmad Abbasi (Virtual University)

Theory of Computation

39 / 40

Designing TMs

Sarmad Abbasi (Virtual University)

Theory of Computation

40 / 40

You might also like