0% found this document useful (0 votes)
118 views

Turing Machine Basics

1. Turing machines are abstract computing devices that can simulate any computer algorithm. They have a tape divided into cells that extends infinitely in both directions, and a read/write head that can move left and right to read and write symbols on the tape. 2. The machine's behavior is defined by its state table which specifies the next state, symbol to write, and head movement based on the current state and symbol read. 3. A Turing machine accepts an input string if it halts in an accepting state after processing the string according to its state table transitions. The accepted language is the set of all accepted strings.

Uploaded by

sanjay prabhu
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
118 views

Turing Machine Basics

1. Turing machines are abstract computing devices that can simulate any computer algorithm. They have a tape divided into cells that extends infinitely in both directions, and a read/write head that can move left and right to read and write symbols on the tape. 2. The machine's behavior is defined by its state table which specifies the next state, symbol to write, and head movement based on the current state and symbol read. 3. A Turing machine accepts an input string if it halts in an accepting state after processing the string according to its state table transitions. The accepted language is the set of all accepted strings.

Uploaded by

sanjay prabhu
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 112

Formal Languages

Turing Machines
The Language Hierarchy

n n n ?
a b c ww ?

Context-Free Languages
n n R
a b ww
Regular Languages
a* a *b *
2
Languages accepted by
Turing Machines
n n n
a b c ww

Context-Free Languages
n n R
a b ww
Regular Languages
a* a *b *
3
A Turing Machine
Tape
...... ......

Read-Write head
Control Unit

4
The Tape

No boundaries -- infinite length


...... ......

Read-Write head

The head moves Left or Right

5
...... ......

Read-Write head

The head at each time step:

1. Reads a symbol
2. Writes a symbol
3. Moves Left or Right
6
Example:
Time 0
...... a b a c ......

Time 1
...... a b k c ......

1. Reads a
2. Writes k
3. Moves Left
7
Time 1
...... a b k c ......

Time 2
...... a f k c ......

1. Reads b
2. Writes f
3. Moves Right
8
The Input String

Input string Blank symbol

......   a b a c    ......

head

Head starts at the leftmost position


of the input string
9
Turing thesis, which maintains that any computational
process, such as those carried out by present-day computers,
can be done on a Turing machine.

The Standard Turing Machine


It can be visualized as
 a single, one-dimensional array of cells, each of which can
hold a single symbol.
 This array extends indefinitely in both directions and is
therefore capable of holding an unlimited amount of
information.
 The information can be read and changed in any order.
 We will call such a storage device a tape because it is
analogous to the magnetic tapes used in older computers.
10
Definition of a Turing Machine

• A Turing machine is an automaton whose temporary storage


is a tape.

• This tape is divided into cells, each of which is capable of


holding one symbol.

• Associated with the tape is a read-write head that can travel


right or left on the tape and that can read and write a single
symbol on each move.

• The automaton that we use as a Turing machine will have


neither an input file nor any special output mechanism.

• Whatever input and output is necessary will be done on the


machine's tape. 11
Turing Machine

12
we assume that Σ ⊆ Γ – {□}, that is, that the input alphabet is a subset of
the tape alphabet, not including the blank. 13
The transition function δ is defined as
δ : Q × Γ → Q × Γ × {L,R}

14
• The whole process may terminate, which we achieve in a Turing
machine by putting it into a halt state.
• A Turing machine is said to halt whenever it reaches a configuration
for which δ is not defined.
• A Turing machine is said to halt whenever it reaches a configuration
for which δ is defined or when string gets accepted.

15
• If this Turing machine is started in state q0 with the symbol a under the
read-write head, the applicable transition rule is δ (q0,a)= (q0,b,R).

• Therefore, the read-write head will replace the a with a ‘b’, then move
right on the tape.

• The machine will remain in state q0.

• Any subsequent a will also be replaced with a ‘b’, but b's will not be
modified.

• When the machine encounters the first blank, it will move left one cell,
then halt in final state q1.

Sequence of moves
16
Transition Graph

Infinite Loop

In this TM does not halt

17
Summarize the main features of our model

1. The Turing machine has a tape that is unbounded in both


directions, allowing any number of left and right moves.

2. The Turing machine is deterministic in the sense that δ


defines at most one move for each configuration.

3. There is no special input file. We assume that at the initial


time the tape has some specified content. Some of this may be
considered input.
4. Similarly, there is no special output device. Whenever the
machine halts, some or all of the contents of the tape may be
viewed as output.

18
q0aa, bq0a, bbq0 □, bq1b

19
The sequence of configurations leading to a halt state will
be called a computation.

starting from the

20
Turing Machines as Language Accepters

The machine is started in the initial state q0 with the read write
head positioned on the leftmost symbol of ω. If, after a
sequence of moves, the Turing machine enters a final state
and halts, then w is considered to be accepted.

The machine can halt in a non-final state or it can enter an infinite


loop and never halt. Any string for which M does not halt is by

definition not in L(M).

21
Example 1:
For Σ = {0,1}, design a Turing machine that accepts the language
denoted by the regular expression 00*.

• Starting at the left end of the input, we read each symbol and check that it is a
0.
• If it is, we continue by moving right.
• If we reach a blank without encountering anything but 0, we terminate
and accept the string
• If the input contains a 1 anywhere, the string is not in L(00*), and we halt
in a nonfinal state.
• To keep track of the computation, two internal states Q= {q0,q1} and one
final state F= {q1} can be assumed.

22
Example 1: a/ a , R □ / □, R
q0 q1 qf
L(aa*) a/a,R

□ a a a □ □

M = (Q, Σ, Γ,δ, q0,, F)


Q= {q0, q1, qf}, Σ = {a}, Γ = {a, □}, F = {qf}
δ= Q x Γ  Q x Γ x {L, R}
δ( q0, a) = (q1, a, R)
δ( q1, a) = (q1, a, R)
δ( q1, □) = (qf, □, R)

ID: q0 a a a Ͱ a q1 a a Ͱ a a q1 a Ͱ a a a q1 □ Ͱ a a a □ qf □

23
Example 2:

L(a( a+b)*) assume some string aba


□ a b a □ □

M = (Q, Σ, Γ,δ, q0,, F), Q= {q0, q1, q2}, Σ = {a,b}, Γ = {a,b,


□}, F = {q2} , δ= Q x Γ  Q x Γ x {L, R}

δ( q0, a) = (q1, a, R)
δ( q1, a) = (q1, a, R)
δ( q1, b) = (q1, b, R)
δ( q1, □) = (q2, □, R)

ID: q0 a b a Ͱ a q1 b a Ͱ a b q1 a Ͱ a b a q1 □ Ͱ a b a □ q2 □
24
a/a, R | b/b, R
Example 3:

L= {w | w is even and Σ = {a,b}, }


q0 q1
B/B, R a/a, R | b/b, R

qf

M = (Q, Σ, Γ,δ, q0,, F), Q= {q0, q1,q2}, Σ = {a,b}, Γ = {a,b, □}, F


= {q2} , δ= Q x Γ  Q x Γ x {L, R}

δ( q0, a) = (q1, a, R) or δ( q0, b) = (q1, b, R)


δ( q1, a) = (qo, a, R) or δ( q1, b) = (q0, b, R)
δ( q0, □) = (q2, □, R)

ID: q0 a b a b Ͱ a q1 b ab Ͱ a b q0 a b Ͱ a b a q1b □ Ͱ a b a b q0
□ Ͱ a b a b □ q2 □
25
Example 4: L= {w | w ϵ Σ = (0+1)*} containing substring 001

0
1 0, 1

q0 0
q1 q2 q3
0 1

δ( q0, 0) = (q1, 0, R) 1/1,R 0/0,R 0/0,R


1/1,R
δ( q0, 1) = (q0, 1, R) q0 q1 q2 q3
0/0,R
δ( q1, 0) = (q2, 0, R) 0/0,R
□ / □,R
δ( q1, 1) = (q0, 1, R) 1/1,R

δ( q2, 0) = (q2, 0, R) qf
δ( q2, 1) = (q3, 1, R)
δ( q3, 0) = (q3, 0, R)
δ( q3, 1) = (q3, 1, R)
δ( q3, □) = (qf, □, R)
26
Input string Blank symbol

......   a b a c    ......

head

Remark: the input string is never empty

27
States & Transitions

Read Write
Move Left

q1 a  b, L q2

Move Right

q1 a  b, R q2
28
Example:
Time 1
......   a b a c    ......

q1
current state

q1 a  b, R q2
29
Time 1
......   a b a c    ......

q1

Time 2
......   a b b c    ......

q2

q1 a  b, R q2
30
Example:
Time 1
......   a b a c    ......

q1

Time 2
......   a b b c    ......

q2

q1 a  b, L q2
31
Example:
Time 1
......   a b a c    ......

q1

Time 2
......   a b b c g   ......

q2

q1   g, R q2
32
Determinism
Turing Machines are deterministic

Allowed Not Allowed


a  b, R q2 a  b, R q2

q1 q1
q3 a  d, L q3
b  d, L

No lambda transitions allowed


33
Partial Transition Function
Example:

......   a b a c    ......

q1

a  b, R q2 Allowed:

q1 No transition
for input symbol c
b  d, L q3
34
Halting

The machine halts if there are


no possible transitions to follow

35
Example:

......   a b a c    ......

q1

a  b, R q2
No possible transition
q1
HALT!!!
b  d, L q3
36
Final States

q1 q2 Allowed

q1 q2 Not Allowed

• Final states have no outgoing transitions

• In a final state the machine halts


37
Acceptance

If machine halts
Accept Input
in a final state

If machine halts
in a non-final state
Reject Input or
If machine enters
an infinite loop
38
Turing Machine Example

Language?

a  a, R

  , L
q0 q1

39
Turing Machine Example

A Turing machine that accepts the language:


aa *

a  a, R

  , L
q0 q1

40
Time 0   a a a  

q0

a  a, R

  , L
q0 q1

41
Time 1   a a a  

q0

a  a, R

  , L
q0 q1

42
Time 2   a a a  

q0

a  a, R

  , L
q0 q1

43
Time 3   a a a  

q0

a  a, R

  , L
q0 q1

44
Time 4   a a a  

q1

a  a, R Halt & Accept

  , L
q0 q1

45
Rejection Example

Time 0   a b a  

q0

a  a, R

  , L
q0 q1
46
Time 1   a b a  

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

  , L
q0 q1
47
Language?

b  b, L
a  a, R

  , L
q0 q1

48
Infinite Loop Example
A Turing machine
for language aa * b( a  b) *

b  b, L
a  a, R

  , L
q0 q1

49
Time 0   a b a  

q0

b  b, L
a  a, R

  , L
q0 q1

50
Time 1   a b a  

q0

b  b, L
a  a, R

  , L
q0 q1

51
Time 2   a b a  

q0

b  b, L
a  a, R

  , L
q0 q1

52
Time 2   a b a  
q0
Time 3   a b a  

Infinite loop
q0
Time 4   a b a  
q0

Time 5   a b a  
q0
53
Because of the infinite loop:

•The final state cannot be reached

•The machine never halts

•The input is not accepted

54
• Starting at the leftmost a, we check it off by replacing it with some
symbol, say x.

• We then let the read-write head travel right to find the leftmost b, which
in turn is checked off by replacing it with another symbol, say y.

• After that, we go left again to the leftmost a, replace it with an x, then


move to the leftmost band replace it with y, and so on.

• Traveling back and forth this way, we match each a with a corresponding
b. If after some time no a's or b's remain, then the string must be in L.

55
Q= {q0,q1,q2,q3,q4},F= {q4}, Σ= {a,b},Γ={a,b, x, y, }.
The transitions can be broken into several parts.

Phase 1:

• The set replaces the leftmost a with an x, then causes the read-write head
to travel right to the first b, replacing it with a y.

• When the y is written, the machine enters a state q2, indicating that an a has
been successfully paired with a b.

56
Final check…. is made to see if all a’s and b’s have been
replaced (to detect input where an a follows a b)

57
58
Another Turing Machine Example

Language?

q4 y  y, R y  y, L
y  y, R a  a, R a  a, L
  , L
y  y, R a  x, R b  y, L
q3 q0 q1 q2
x  x, R
59
Another Turing Machine Example
n n
Turing machine for the language {a b }

q4 y  y, R y  y, L
y  y, R a  a, R a  a, L
  , L
y  y, R a  x, R b  y, L
q3 q0 q1 q2
x  x, R
60
Time 0  a a b b  

q0

q4 y  y, R y  y, L
y  y, R a  a, R a  a, L
  , L
y  y, R a  x, R b  y, L
q3 q0 q1 q2
x  x, R
61
Time 1  x a b b  

q1

q4 y  y, R y  y, L
y  y, R a  a, R a  a, L
  , L
y  y, R a  x, R b  y, L
q3 q0 q1 q2
x  x, R
62
Time 2  x a b b  

q1

q4 y  y, R y  y, L
y  y, R a  a, R a  a, L
  , L
y  y, R a  x, R b  y, L
q3 q0 q1 q2
x  x, R
63
Time 3  x a y b  

q2

q4 y  y, R y  y, L
y  y, R a  a, R a  a, L
  , L
y  y, R a  x, R b  y, L
q3 q0 q1 q2
x  x, R
64
Time 4  x a y b  

q2

q4 y  y, R y  y, L
y  y, R a  a, R a  a, L
  , L
y  y, R a  x, R b  y, L
q3 q0 q1 q2
x  x, R
65
Time 5  x a y b  

q0

q4 y  y, R y  y, L
y  y, R a  a, R a  a, L
  , L
y  y, R a  x, R b  y, L
q3 q0 q1 q2
x  x, R
66
Time 6  x x y b  

q1

q4 y  y, R y  y, L
y  y, R a  a, R a  a, L
  , L
y  y, R a  x, R b  y, L
q3 q0 q1 q2
x  x, R
67
Time 7  x x y b  

q1

q4 y  y, R y  y, L
y  y, R a  a, R a  a, L
  , L
y  y, R a  x, R b  y, L
q3 q0 q1 q2
x  x, R
68
Time 8  x x y y  

q2

q4 y  y, R y  y, L
y  y, R a  a, R a  a, L
  , L
y  y, R a  x, R b  y, L
q3 q0 q1 q2
x  x, R
69
Time 9  x x y y  

q2

q4 y  y, R y  y, L
y  y, R a  a, R a  a, L
  , L
y  y, R a  x, R b  y, L
q3 q0 q1 q2
x  x, R
70
Time 10  x x y y  

q0

q4 y  y, R y  y, L
y  y, R a  a, R a  a, L
  , L
y  y, R a  x, R b  y, L
q3 q0 q1 q2
x  x, R
71
Time 11  x x y y  

q3

q4 y  y, R y  y, L
y  y, R a  a, R a  a, L
  , L
y  y, R a  x, R b  y, L
q3 q0 q1 q2
x  x, R
72
Time 12  x x y y  

q3

q4 y  y, R y  y, L
y  y, R a  a, R a  a, L
  , L
y  y, R a  x, R b  y, L
q3 q0 q1 q2
x  x, R
73
Time 13  x x y y  

q4
Halt & Accept

q4 y  y, R y  y, L
y  y, R a  a, R a  a, L
  , L
y  y, R a  x, R b  y, L
q3 q0 q1 q2
x  x, R
74
Observation:

If we modify the
machine for the language n n
{a b }

we can easily construct


n n n
a machine for the language {a b c }

75
we can easily construct a machine for the language
{a nb nc n }
y/y,R z/z,R
a/a,R b/b,R

a/x,R z/z,L
b/y,R
q0 q1 q2 q3 y/y,L
c/z,L b/b,L
y/y,R a/a,L
x/x,R
B/B,R
q4 qf

y/y, R
z/z,R

76
Turing Machines as Transducers

Example 1:
Given two positive integers x and y, design a Turing machine that computes x + y.

• We will assume that w(x) and w(y) are on the tape in unary notation, separated by a
single 0, with the read-write head on the leftmost symbol of w(x).

• After the computation, w (x+ y) will be on the tape followed by a single 0, and the
read-write head will be positioned at the left end of the result.

77
Integer Domain

Decimal: 5

Binary: 101

Unary: 11111

We prefer unary representation:

easier to manipulate with Turing machines


78
To achieve this, we construct M =(Q,Σ,Γ,δ,q0, ,F), with Q = {q0,q1,q2,q3,q4},
F= {q4},

Instantaneous Description

79
Execution Example: Time 0
x y
x  11 (2)
 1 1 0 1 1 
y  11 (2)
q0

Final Result
x y
 1 1 1 1 0 

q4
80
Time 0  1 1 0 1 1 

q0

1  1, R 1  1, R 1  1, L

q0 0  1, R q1   , L q 1  0, L q3
2

  , R
q4
81
Time 1  1 1 0 1 1 

q0

1  1, R 1  1, R 1  1, L

q0 0  1, R q1   , L q 1  0, L q3
2

  , R
q4
82
Time 2  1 1 0 1 1 

q0

1  1, R 1  1, R 1  1, L

q0 0  1, R q1   , L q 1  0, L q3
2

  , R
q4
83
Time 3  1 1 1 1 1 

q1

1  1, R 1  1, R 1  1, L

q0 0  1, R q1   , L q 1  0, L q3
2

  , R
q4
84
Time 4  1 1 1 1 1 

q1

1  1, R 1  1, R 1  1, L

q0 0  1, R q1   , L q 1  0, L q3
2

  , R
q4
85
Time 5  1 1 1 1 1 

q1

1  1, R 1  1, R 1  1, L

q0 0  1, R q1   , L q 1  0, L q3
2

  , R
q4
86
Time 6  1 1 1 1 1 

q2

1  1, R 1  1, R 1  1, L

q0 0  1, R q1   , L q 1  0, L q3
2

  , R
q4
87
Time 7  1 1 1 1 0 

q3

1  1, R 1  1, R 1  1, L

q0 0  1, R q1   , L q 1  0, L q3
2

  , R
q4
88
Time 8  1 1 1 1 0 

q3

1  1, R 1  1, R 1  1, L

q0 0  1, R q1   , L q 1  0, L q3
2

  , R
q4
89
Time 9  1 1 1 1 0 

q3

1  1, R 1  1, R 1  1, L

q0 0  1, R q1   , L q 1  0, L q3
2

  , R
q4
90
Time 10  1 1 1 1 0 

q3

1  1, R 1  1, R 1  1, L

q0 0  1, R q1   , L q 1  0, L q3
2

  , R
q4
91
Time 11  1 1 1 1 0 

q3

1  1, R 1  1, R 1  1, L

q0 0  1, R q1   , L q 1  0, L q3
2

  , R
q4
92
Time 12  1 1 1 1 0 

q4

1  1, R 1  1, R 1  1, L

q0 0  1, R q1   , L q 1  0, L q3
2

  , R
HALT & accept q4
93
Design a Turing machine that copies strings of 1’s. More precisely, find
a machine that performs the computation
for any w ∈{1}+.

To solve the problem, we implement the following intuitive process:


1. Replace every 1 by an x.
2. Find the rightmost x and replace it with 1.
3. Travel to the right end of the current nonblank region and create a 1 there.
4. Repeat Steps 2 and 3 until there are no more x's.

94
Transition Graph

Instantaneous Description

95
A function may have many parameters:

Example: Addition function

f ( x, y )  x  y

96
Definition:

A function f is computable if
there is a Turing Machine M such that:

Initial configuration Final configuration


 w   f (w) 

q0 initial state q f final state

For all w D Domain


97
In other words:
A function f is computable if
there is a Turing Machine M such that:


q0 w  q f f ( w)

Initial Final
Configuration Configuration

For all w D Domain


98
Example

The function f ( x, y )  x  y is computable

x, y are integers

Turing Machine:

Input string: x0 y unary

Output string: xy0 unary

99
x y

Start  1 1  1 0 1  1 

q0 initial state

x y

Finish  1 1  1 1 0 
q f final state
100
Another Example

The function f ( x)  2 x is computable

x is integer

Turing Machine:

Input string: x unary

Output string: xx unary


101
x

Start  1 1  1 

q0 initial state

2x

Finish  1 1  1 1 1 
q f final state
102
Turing Machine Pseudocode for f ( x)  2 x

• Replace every 1 with $

• Repeat:
• Find rightmost $, replace it with 1

• Go to right end, insert 1

Until no more $ remain

103
Turing Machine for f ( x)  2 x

1  $, R 1  1, L 1  1, R

q0   , L q1 $  1, R q2
  , R   1, L
q3
104
Example
Start Finish
 1 1   1 1 1 1 
q0 q3
1  $, R 1  1, L 1  1, R

q0   , L q1 $  1, R q2
  , R   1, L
q3
105
• Instead of matching a's and b’s, we match each 1 on the left of
the dividing 0 with the 1 on the right.

• At the end of the matching, we will have on the tape either

or

106
• In the first case, when we attempt to match another 1, we encounter
the blank at the right of the working space. This can be used as a
signal to enter the state q y.
Case 1 :

• In the second case, we still find a 1 on the right when all 1’s on the left
have been replaced. We use this to get into the other state q n.
Case 2:

Try …
Design a Turing machine that multiplies two positive integers
in unary notation.
107
Combining Turing Machines

108
Block Diagram

Turing
input output
Machine

109
Example: x  y if x  y
f ( x, y ) 
0 if x  y

x, y
Adder x y
x, y x y
Comparer

x y Eraser 0

110
111
Consider the macroinstruction if a then qj else qk, with the following
interpretation.

• If the Turing machine reads an a, then regardless of its current state, it is to


go into state qj without changing the tape content or moving the read-write
head.

• If the symbol read is not an a, the machine is to go into state qk without


changing anything.

112

You might also like