You are on page 1of 82

Turing Machines

Invented by Alan Turing in 1936.


A simple mathematical model of a general
purpose computer.
It is capable of performing any calculation
which can be performed by any computing
machine.

The Language Hierarchy


n n n

a b c ?

ww ?

Context-Free Languages
n n
R
a b

ww

Regular Languages

a*

a *b *

Languages accepted by
Turing Machines

ww

n n n

a b c

Context-Free Languages
n n
R
a b

ww

Regular Languages

a * a *b *

NDPA

Finite
Automata

Tape
......

A Turing Machine
......
Read-Write head

Control Unit

The Tape
No boundaries -- infinite length
......

......
Read-Write head

The head moves Left or Right

......

......
Read-Write head

The head at each time step:


1. Reads a symbol
2. Writes a symbol
3. Moves Left or Right

Example:

Time 0

......

a b a c

Time 1

......
1. Reads
2. Writes

a b k c

a
k

3. Moves Left

......

......

Time 1

......

a b k c

Time 2

......
1. Reads
2. Writes

a f k c

b
f

3. Moves Right

......

......

The Input String


Input string
......

a b a c

Blank symbol

......

head
Head starts at the leftmost position
of the input string

Are treated as left and right brackets for the


input written on the tape.

States & Transitions


Read

q1

Write

a b, L

Move Left

q2
Move Right

q1

a b, R

q2

Example:
Time 1
......

a b a c
q1

current state

q1

a b, R

q2

......

......

Time 1

a b a c

......

......

q1
......

Time 2

a b b c
q2

q1

a b, R

q2

Example:
......

Time 1

a b a c

......

......

q1
......

Time 2

a b b c
q2

q1

a b, L

q2

Example:
......

Time 1

a b a c

......

q1
......

Time 2

a b b c g
q2

q1

g, R

q2

......

Determinism

Turing Machines are deterministic

Not Allowed

Allowed

a b, R

q2

a b, R

q2

a d, L

q3

q1

q1
b d, L

q3

No lambda transitions allowed

Partial Transition Function

Example:
......

a b a c

......

q1
a b, R

q2

q1
b d, L

q3

Allowed:
No transition
for input symbol

Halting
The machine halts if there are
no possible transitions to follow

Example:

......

a b a c

......

q1
a b, R

q2

q1
b d, L

q3

No possible transition
HALT!!!

Final States
q1

q2

Allowed

q1

q2

Not Allowed

Final states have no outgoing transitions


In a final state the machine halts

Acceptance
Accept Input

If machine halts
in a final state

Reject Input

If machine halts
in a non-final state
or
If machine enters
an infinite loop

Turing Machine Example


A Turing machine that accepts the language:

aa *
a a, R
q0

, L

q1

Time 0

a a a
q0

a a, R
q0

, L

q1

Time 1

a a a
q0

a a, R
q0

, L

q1

Time 2

a a a
q0

a a, R
q0

, L

q1

Time 3

a a a
q0

a a, R
q0

, L

q1

Time 4

a a a

q1

a a, R
q0

Halt & Accept

, L

q1

Rejection Example
Time 0

a b a
q0

a a, R
q0

, L

q1

Time 1

a b a
q0
No possible Transition
Halt & Reject
a a, R

q0

, L

q1

Infinite Loop Example

b b, L
a a, R
q0

, L

q1

Time 0

a b a
q0

b b, L
a a, R
q0

, L

q1

Time 1

a b a
q0

b b, L
a a, R
q0

, L

q1

Time 2

a b a
q0

b b, L
a a, R
q0

, L

q1

Time 2

a b a
q0

Time 3

a b a
q0

Time 4

a b a
q0

Time 5

a b a

... Infinite Loop

q0

Because of the infinite loop:


The final state cannot be reached
The machine never halts
The input is not accepted

Another Turing Machine


Example
n

Turing machine for the language

y y, R
q3

q4
, L

y y, R

q0

y y, R
a a, R

a x, R

q1

{a b }

y y, L
a a, L

b y, L

x x, R

q2

a a b b

Time 0

q0

y y, R
q3

q4
, L

y y, R

q0

y y, R
a a, R

a x, R

q1

y y, L
a a, L

b y, L

x x, R

q2

x a b b

Time 1

q1

y y, R
q3

q4
, L

y y, R

q0

y y, R
a a, R

a x, R

q1

y y, L
a a, L

b y, L

x x, R

q2

x a b b

Time 2

q1

y y, R
q3

q4
, L

y y, R

q0

y y, R
a a, R

a x, R

q1

y y, L
a a, L

b y, L

x x, R

q2

x a y b

Time 3

q2

y y, R
q3

q4
, L

y y, R

q0

y y, R
a a, R

a x, R

q1

y y, L
a a, L

b y, L

x x, R

q2

x a y b

Time 4

q2

y y, R
q3

q4
, L

y y, R

q0

y y, R
a a, R

a x, R

q1

y y, L
a a, L

b y, L

x x, R

q2

x a y b

Time 5

q0

y y, R
q3

q4
, L

y y, R

q0

y y, R
a a, R

a x, R

q1

y y, L
a a, L

b y, L

x x, R

q2

x x y b

Time 6

q1

y y, R
q3

q4
, L

y y, R

q0

y y, R
a a, R

a x, R

q1

y y, L
a a, L

b y, L

x x, R

q2

x x y b

Time 7

q1

y y, R
q3

q4
, L

y y, R

q0

y y, R
a a, R

a x, R

q1

y y, L
a a, L

b y, L

x x, R

q2

x x y y

Time 8

q2

y y, R
q3

q4
, L

y y, R

q0

y y, R
a a, R

a x, R

q1

y y, L
a a, L

b y, L

x x, R

q2

x x y y

Time 9

q2

y y, R
q3

q4
, L

y y, R

q0

y y, R
a a, R

a x, R

q1

y y, L
a a, L

b y, L

x x, R

q2

x x y y

Time 10

q0

y y, R
q3

q4
, L

y y, R

q0

y y, R
a a, R

a x, R

q1

y y, L
a a, L

b y, L

x x, R

q2

x x y y

Time 11

q3

y y, R
q3

q4
, L

y y, R

q0

y y, R
a a, R

a x, R

q1

y y, L
a a, L

b y, L

x x, R

q2

x x y y

Time 12

q3

y y, R
q3

q4
, L

y y, R

q0

y y, R
a a, R

a x, R

q1

y y, L
a a, L

b y, L

x x, R

q2

x x y y

Time 13

q4
Halt & Accept

y y, R
q3

q4
, L

y y, R

q0

y y, R
a a, R

a x, R

q1

y y, L
a a, L

b y, L

x x, R

q2

Observation:
If we modify the
machine for the language

we can easily construct


a machine for the language

n n

{a b }

n n n

{a b c }

Formal Definitions
for
Turing Machines

Transition Function

q1

c d, L

q2

(q1, c) (q2 , d , L)

Turing Machine:

States

Input
alphabet

Tape
alphabet

M (Q, , , , q0 , , F )
A partial
Transition
function

Initial
state

Final
states
Blank : a special symbol
Of

Configuration
c a b a
q1

Instantaneous description:

ca q1 ba

Time 4

Time 5

x a y b
q2

A Move:

x a y b
q0

q2 xayb x q0 ayb

Time 4

x a y b

Time 5

x a y b
q0

q2
Time 6

x x y b
q1

Time 7

x x y b
q1

q2 xayb x q0 ayb xx q1 yb xxy q1 b

q2 xayb x q0 ayb xx q1 yb xxy q1 b

Equivalent notation:

q2 xayb xxy q1 b

q0 w

Initial configuration:

Input string

w
a a b b
q0

The Accepted Language


For any Turing Machine

L( M ) {w :

q0 w x1 q f x2 }

Initial state

Final state

Standard Turing Machine

The machine we described is the standard:


Deterministic
Infinite tape in both directions
Tape is the input/output file

Design a Turing machine to recognize all


strings in which 010 is present as a
substring.
0,0,R
0,0,R
q0

1,1, R

1,1,R
q1

1,1,R

0,0,R
q2

DFA for the previous language


0

q0

q1

0
q2

1
0,1

Turing machine for odd no of 1s

1, 1 , R

1, b , R

1, b , R

Recursively Enumerable
and
Recursive
Languages

Definition:
A language is recursively enumerable
if some Turing machine accepts it

Let

be a recursively enumerable language

and

the Turing Machine that accepts it

For string

w:

if

w L

then

halts in a final state

if

w L

then

halts in a non-final state


or loops forever

Definition:
A language is recursive
if some Turing machine accepts it
and halts on any input string

In other words:
A language is recursive if there is
a membership algorithm for it

Let

be a recursive language

and

the Turing Machine that accepts it

For string

w:

if

w L

then

halts in a final state

if

w L

then

halts in a non-final state

We will prove:
1. There is a specific language
which is not recursively enumerable
(not accepted by any Turing Machine)

2. There is a specific language


which is recursively enumerable
but not recursive

Non Recursively Enumerable

Recursively Enumerable

Recursive

We will first prove:


If a language is recursive then
there is an enumeration procedure for it

A language is recursively enumerable


if and only if
there is an enumeration procedure for it

The Chomsky Hierarchy

Unrestricted Grammars:
Productions

uv

String of variables
and terminals

String of variables
and terminals

Example unrestricted grammar:

S aBc
aB cA
Ac d

Theorem:
A language L is recursively enumerable
if and only if L is generated by an
unrestricted grammar

Context-Sensitive Grammars:
Productions

uv

String of variables
and terminals
and:

|u | |v|

String of variables
and terminals

The language

n n n

{a b c }

is context-sensitive:

S abc | aAbc
Ab bA
Ac Bbcc
bB Bb
aB aa | aaA

The language

n n n

{a b c }

is context-sensitive:

S abc | aAbc
Ab bA
Ac Bbcc
bB Bb
aB aa | aaA

Theorem:
A language

is context sensistive
if and only if
is accepted by a Linear-Bounded automaton

Observation:
There is a language which is context-sensitive
but not recursive

The Chomsky Hierarchy


Non-recursively enumerable
Recursively-enumerable
Recursive
Context-sensitive
Context-free
Regular