ch3_turingmachine
TRANSCRIPT
-
8/10/2019 Ch3_TuringMachine
1/32
thuyt tnh ton -- Ch.3 - My Turing
L thuyL thuyt tt tnh tonh tonn(Theory of Computation)(Theory of Computation)
PGS.TS. Phan Huy KhPGS.TS. Phan Huy Khnhnh
[email protected]@dut.udn.vn,, [email protected]@gmail.com
Chng 3Chng 3
2/189
CHNGCHNG 33
M HNH MM HNH MY TURINGY TURING
(Turing Machines Model)(Turing Machines Model)
Alan Mathison TuringAlan Mathison Turing ((23/06/191223/06/1912 07/06/195407/06/1954))nhnh toton hn hc,logic hc,logic hcc vv mmt m ht m hcc ngngiiAnhAnh
ththng ng c xem lc xem lcha cha cca nga ngnhnh Khoa hKhoa hc Mc My ty tnhnh
3/189
TheThe ChomskyChomskys Languages LanguageHierarchyHierarchy
Context-Free LanguagesnnbaPDA Rww
Context-Sensitive Languages
anbncn ? ww ?
*a
Regular Languages
**ba
DFA
Languages accepted by
Turing Machines
*4/189
Handicapped machines
DFA (Deterministic Finite Automaton) limitationsTape head moves only one direction
2-way DFA has equivalent power
Tape is read-only
Tape length is a constant
PDA (Push-Down Deterministic Finite Automaton) limitationsTape head moves only one direction
2-way PDA has a little more power (can accept { ww | w* }
Tape is read-only, but stack is writable
Stack has only LIFO (last-in, first-out) access
Tape length is constant, but stack is not bounded
What about:Writable, 2-way tape?
Random-accessstack?
5/189
Computability
Computible (there is an algorithm)Equivalence of DFAs
String membership in a regular language
Determining valid executions of a PDA
UncomputibleEquivalence of CFGs
Emptiness of the complement of a CFL(emptiness of CFL is computible)
Whether a C program ever terminates
How to prove it?
6/189
Introduction to Turing MachinesIntroduction to Turing Machines ((TMTM))TM: a formal model of a (digital) computerTM: a formal model of a (digital) computer
running a particular programrunning a particular program
The TM is the invention ofThe TM is the invention of Alan TuringAlan Turing, 1936, 1936
Other formalisms equivalent :Other formalisms equivalent :
Random Access MachinesRandom Access Machines
Partial recursive functions (Partial recursive functions (KleeneKleene, 1952), 1952)
--calculuscalculus ((ChurchChurch, 1936), 1936)
Post systems (Post systems (PostPost, 1943), 1943)
etc...etc...
All these were preceded by the work ofAll these were preceded by the work of Kurt GodelKurt Godel, 1931,, 1931,which in his incompleteness theorem showed that there was nowhich in his incompleteness theorem showed that there was noway for a computer to answer all mathematical problemsway for a computer to answer all mathematical problems
-
8/10/2019 Ch3_TuringMachine
2/32
thuyt tnh ton -- Ch.3 - My Turing
7/189
M tM t ttng qung qut mt my Turingy Turing
MMt bng v ht bng v hn cn c hai phhai pha tra tri vi v phphii
MMttuucc--ghighicc hayhay ghighi lnln bngbng,, theotheo haihai chichiuu
MMtt bb nhnh phph hhu hu hn xn xc c nh trnh trng thng thii
MMt cu vt cu vo o c c t trn bngt trn bng
MMt chng trt chng trnh lnh lm hom hot t ng mng my Turingy Turing
Blank symbol
...... a b ca
Input string
Read-Write head
Tape
8/189
A Turing MachineA Turing Machine
9/189
M tM t chi tichi tit ct cc thc thnh phnh phn TMn TM
Bng lBng lm vim vic :c :c chia thc chia thnh cnh cc lin tic lin tipp
MMi chi cha ma mt k tt k tnno o aa SS
S cS c chcha k ta k t trtrngng (blank symbol),, hohocc ##
PhPhn cu cn cu c chch (usable part)(usable part) wwS*S*t git giaa t nht nhtt hai k thai k t##NgNgi ta vii ta vitt #w#w##
u u ccghi :ghi :c hay ghi (lc hay ghi (lm thay m thay i) ni) ni dung ti dung tng cng ca bnga bng
Di chuyDi chuyn mn mt vt v trtrqua phqua phi, hoi, hoc trc tri,i, tngtng ng vng vi mi mt t
TTi mi mi thi thi ii im, chm, ch duy nhduy nht mt mt t c c c, hoc, hoc c c ghic ghi
10/189
M tM t chi tichi tit ct cc thc thnh phnh phn (2)n (2)
MMt bt b nhnh phph hhu hu hn :n :
TiTip cp cn n c nc ni dung ti dung ti mi mi thi thi ii imm
CC thththay thay i i c nc ni dungi dung
BB nhnh phphc lic lit k lt k lc c nh nghnh nghaa
MMi phi phn tn tcca ba b nhnh phph xxc c nh mnh mt trt trng thng thii
Chng trChng trnh :nh :
TTp hp hp hp hu hu hn cn cc quy tc quy tcc
LLm thay m thay i ni ni dung trn bng vi dung trn bng v nni dung bi dung b nhnh phph
11/189
A Turing MachineA Turing Machine
. . .. . .
Input tape
Read-Write head
Control Unit / Transition Diagram
Input String w *
a ba
12/189
The TapeThe Tape
......
Read-Write head
The head moves Left or Right
No boundaries -- infinite length
The head at each time step:
1. Reads a symbol
2. Writes a symbol
3. Moves Left or Right
-
8/10/2019 Ch3_TuringMachine
3/32
thuyt tnh ton -- Ch.3 - My Turing
13/189
Example Moves Left :
Time 0
Time 1
...... a a cb
...... a b k c
1. Reads
2. Writes
a
k
3. Moves Left14/189
Time 1
...... a b k c
Time 2
...... a k cf
1. Reads
2. Writes
b
f
3. Moves Right
Example Moves Right :
15/189
The Input StringThe Input String
Blank symbol
head
...... a b ca
Head starts at the leftmost position
of the input string
Input string
Remark: the input string is never empty16/189
States & TransitionsStates & Transitions
1q 2qLba ,
Read Write Move Left
1q 2qRba ,
Move Right
17/189
Example Moves Right :
1q 2qRba ,
...... a b caTime 1
1qcurrent state
...... a b cb
Time 2
2q
b
a
18/189
1q 2qLba ,
...... a b cTime 1
1qcurrent state
...... a b cb
Time 2
2q
Example Moves Left :
b
a
-
8/10/2019 Ch3_TuringMachine
4/32
-
8/10/2019 Ch3_TuringMachine
5/32
thuyt tnh ton -- Ch.3 - My Turing
25/189
AcceptanceAcceptance
Accept Input If machine halts
in a final state
Reject Input
If machine haltsin a non-final state
or
If machine enters
an infinite loop
26/189
Turing MachineTuring Machine Example (1)Example (1)
A Turing machine that accepts the language:
0q
Raa ,
L,1q
aa* = a+
(This Turing machine mimic a DFA)
27/189
Time 0 aa
0q
a
aaTime 1
0q
a
Turing MachineTuring Machine Example (2)Example (2)
0q L,
1q
a a, R
28/189
aaTime 2
0q
a
0q L,
1q
a a, R
Turing MachineTuring Machine Example (3)Example (3)
aaTime 3
0q
a
a a, R
29/189
aaTime 4
1q
a
Halt & Accept
Turing MachineTuring Machine Example (4)Example (4)
0q
Raa ,
L,1q
30/189
Rejection Example (1)
baTime 0
0q
a
0q
Raa ,
L,1q
-
8/10/2019 Ch3_TuringMachine
6/32
thuyt tnh ton -- Ch.3 - My Turing
31/189
baTime 1
0q
a
No possible Transition
Halt & Reject
Rejection Example (2)
0q
Raa ,
L,1q
32/189
Infinite LoopInfinite Loop Example (1)Example (1)
A Turing machine
for language *)(* babaa ++
0q
Raa ,
L,1q
Lbb ,
33/189
baTime 0
0q
a
Infinite LoopInfinite Loop Example (2)Example (2)
0q
Raa ,
L,1q
Lbb ,
34/189
0q
baTime 1 a
Infinite LoopInfinite Loop Example (3)Example (3)
0q
Raa ,
L,1q
Lbb ,
35/189
baTime 2
0q
a
0q
Raa ,
L,1q
Lbb ,
Infinite LoopInfinite Loop Example (4)Example (4)
36/189
baTime 2
0q
a
baTime 30q
a
baTime 4
0q
a
baTime 5
0q
a
Infinite LoopInfinite Loop Example (5)Example (5)
-
8/10/2019 Ch3_TuringMachine
7/32
thuyt tnh ton -- Ch.3 - My Turing
37/189
Because of the infinite loop:
The final state cannot be reached
The machine never halts
The input is not accepted
InfiniteInfinite Loop ResultLoop Result
38/189
T= Mt cch khc m t ngn gn TM = {1, #}, hoc = {1} Nu khng mun chk ttrng ca bngQ = {q1, q2, q3}P= { q1, 1 1, R, q2,
q2, 1 1, R, q1,q1, # #, L, q3,q3, 1 1, L, q3,q3, 1 1, R, q3,q3, 1 #, R, q1 }
Another TM ExampleAnother TM Example
1|1, R
1q
3q 1|1, L
2q1|1, R
1|1, R
1|#, R #|#, L
c mt s chn s 1, v li q1
Tha nhn cu c s chn s 1Bi tp : M t hot ngon nhn cu 111111 ?
Bi tp : M t hot ngon nhn cu 111111 ?
39/189
TM for the language aTM for the language annbbnn (1)(1)
0q
1q 2q
3q
Ra ,
Raa ,
Ryy ,
Lyb ,
Laa ,
Lyy ,
Rx ,
Ryy ,
Ryy , 4q
L,
40/189
Time 0 ba
0q
a b
TM for the language aTM for the language annbbnn (2)(2)
0q
1q 2q
3q
Ra ,
Raa ,
Ryy ,
Lyb ,
Laa ,
Lyy ,
Rx,
Ryy ,
Ryy , 4q
L,
X, Y
41/189
b
1q
a b Time 1
TM for the language aTM for the language annbbnn (3)(3)
0q 1q 2q3q Ra ,
Raa ,
Ryy ,
Lyb ,
Laa ,
Lyy ,
Rx ,
Ryy ,
Ryy , 4q
L,
42/189
bx
1q
a b Time 2
TM for the language aTM for the language annbbnn (4)(4)
0q 1q 2q3q Ra ,
Raa ,
Ryy ,
Lyb ,
Laa ,
Lyy ,
Rx,
Ryy ,
Ryy , 4q
L,
-
8/10/2019 Ch3_TuringMachine
8/32
thuyt tnh ton -- Ch.3 - My Turing
43/189
y
2q
a b Time 3
TM for the language aTM for the language annbbnn (5)(5)
0q 1q 2q3q Ra ,
Raa ,
Ryy ,
Lyb ,
Laa ,
Lyy ,
Rx ,
Ryy ,
Ryy , 4q
L,
44/189
yx
2q
a b Time 4
TM for the language aTM for the language annbbnn (6)(6)
0q 1q 2q3q Ra ,
Raa ,
Ryy ,
Lyb ,
Laa ,
Lyy ,
Rx,
Ryy ,
Ryy , 4q
L,
45/189
0q
a b Time 5
TM for the language aTM for the language annbbnn (7)(7)
0q
1q 2q
3q
Ra ,
Raa ,
Ryy ,
Lyb ,
Laa ,
Lyy ,
Rx ,
Ryy ,
Ryy , 4q
L,
46/189
yx
1q
x b Time 6
TM for the language aTM for the language annbbnn (8)(8)
0q
1q 2q
3q
Ra ,
Raa ,
Ryy ,
Lyb ,
Laa ,
Lyy ,
Rx,
Ryy ,
Ryy , 4q
L,
47/189
y
1q
b Time 7
TM for the language aTM for the language annbbnn (9)(9)
0q 1q 2q3q Ra ,
Raa ,
Ryy ,
Lyb ,
Laa ,
Lyy ,
Rx ,
Ryy ,
Ryy , 4q
L,
48/189
yx x y
2q
Time 8
TM for the language aTM for the language annbbnn (10)(10)
0q 1q 2q3q Ra ,
Raa ,
Ryy ,
Lyb ,
Laa ,
Lyy ,
Rx,
Ryy ,
Ryy , 4q
L,
-
8/10/2019 Ch3_TuringMachine
9/32
thuyt tnh ton -- Ch.3 - My Turing
49/189
y y
2q
Time 9
TM for the language aTM for the language annbbnn (11)(11)
0q 1q 2q3q Ra ,
Raa ,
Ryy ,
Lyb ,
Laa ,
Lyy ,
Rx ,
Ryy ,
Ryy , 4q
L,
50/189
yx
0q
x y Time 10
TM for the language aTM for the language annbbnn (12)(12)
0q 1q 2q3q Ra ,
Raa ,
Ryy ,
Lyb ,
Laa ,
Lyy ,
Rx,
Ryy ,
Ryy , 4q
L,
51/189
y
3q
y Time 11
TM for the language aTM for the language annbbnn (13)(13)
0q
1q 2q
3q
Ra ,
Raa ,
Ryy ,
Lyb ,
Laa ,
Lyy ,
Rx ,
Ryy ,
Ryy , 4q
L,
52/189
yx
3q
x y Time 12
TM for the language aTM for the language annbbnn (14)(14)
0q
1q 2q
3q
Ra ,
Raa ,
Ryy ,
Lyb ,
Laa ,
Lyy ,
Rx,
Ryy ,
Ryy , 4q
L,
53/189
Halt & Accept
y
4q
y Time 13
TM for the language aTM for the language annbbnn (15)(15)
0q 1q 2q3q Ra ,
Raa ,
Ryy ,
Lyb ,
Laa ,
Lyy ,
Rx ,
Ryy ,
Ryy , 4q
L,
54/189
If we modify the TM
for the Context-Free Language
we can easily construct a TMfor the Context-Sensitive Language
Observation:
}{ nnba
}{ nnn
cba
-
8/10/2019 Ch3_TuringMachine
10/32
thuyt tnh ton -- Ch.3 - My Turing
55/189
TMTMAcceptingAccepting aannbbnnccnn,, nn11
input abc:q0abcxq1bc xyq2c xq3yz q3xyz xq0yz xyq4z xyzq5B xyzBq6B
input a2b2c2:q0 a
2b2c2xaq1b2c2 xayq2bc2 xaybq2c2 xayq3bzc xaq3ybzc xq3aybzc q3xaybzcxq0aybzc xxq1ybzc xxyyq2zc xxyyq3zz xq3xyyzz xxq0yyzz xxyyq4zz xxyyzzq5B xxyyzzBq6B
z/z, L ; b/b, Ly/y, L ; a/a, L
Idea: Finding matchinga,b,c and replacing by x,y,z
Idea: Finding matchinga,b,c and replacing by x,y,z
accepting matched x,y,z
Algorithm
q0
q4
q1 q2 q3
q6q5
a/x, R b/y, R c/z, L
y/y, R
z/z, R #/#, L
a/a, R ; y/y, R
y/y, R z/z, R
b/b, R ; z/z, R
x/x, R
56/189
AlgorithmAlgorithm forforAcceptingAccepting aannbbnnccnn,, nn
11
BeginBeginWhileWhile Cha gCha gp xp xDoDo
GGp a, ghi xp a, ghi x;; DoDoqua Rqua RWhileWhileCha gCha gp bp bOdOdGGp b, ghi yp b, ghi y;; DoDoqua Rqua RWhileWhileCha gCha gp cp cOdOdGGp c, ghi zp c, ghi z;; DoDoqua Lqua LWhileWhileCha gCha gp xp xOdOd
Qua RQua R
OdOdDoDoQua RQua RWhileWhileGGp yp yOdOdDoDoQua RQua RWhileWhileGGp zp zOdOdIfIf GGp #p #ThenThen Qua L,Qua L,YesYesElseElse NoNoFiFi
EndEnd
q0
q4
q1 q2 q3
q6q5
a/x, R b/y, R c/z, L
y/y, R
z/z, R #/#, L
a/a, R,y/y, R
y/y, R z/z, R
b/b, R,z/z, R
x/x, R
z/z, L,b/b, Ly/y, L,a/a, L
Formal DefinitionsFormal Definitionsforfor
Turing MachinesTuring Machines
58/189
Transition Function
1q 2qRba ,
),,(),( 21 Rbqaq =
A Move to right:
1q 2qLdc ,
),,(),( 21 Ldqcq =
A Move to left:
59/189
Turing Machine:
),,,,,,( 0 FqQ =
States
Input
alphabetTape
alphabet
Transitionfunction
Initialstate blank
Set ofFinal states
60/189
ConfigurationConfiguration
ba
1q
a
Instantaneous description:
c
baqca 1
-
8/10/2019 Ch3_TuringMachine
11/32
thuyt tnh ton -- Ch.3 - My Turing
61/189
yx
2q
a b
Time i
yx
0q
a b
Time i+1
A Move:
TransitionTransition
aybqx 0xaybq2
62/189
x
2q
a b
Time 1
xaybq2
yx
1q
x b
Time 4
bqxxy 1
yx
1q
x b
Time 3
ybqxx 1
yx
0q
a b
Time 2
aybqx 0
Transition processTransition process
63/189
Equivalent notation:
Transition processTransition process
bqxxyybqxxaybqxxaybq 1102
bqxxyxaybq 12
64/189
Initial configuration:
ba
0q
a b
wInput string
NotationNotation
wq0
65/189
The Accepted LanguageThe Accepted Language
For any Turing Machine (TM) M:
Initial state Final state
} |{)( 210 xqxwqwML f=
qfF
L(M) is the Language Accepted by TM M
66/189
TM computes palindromes
a|a,Rb|b, R
q2 q3
q7
# | #, L
a|#, R
q1
a|a, Rb|b, R
q5 q6#|#, L
b|#,R
q4
a|#, L
b|#, L
#|#, R
#|#, R
#|#, R
Odd palindrome
Odd palindrome
#|#, R
Even palindrome
a|a, Lb|b, L
Ex1: w=abbbba?Ex2: Draw M: L(M)=w#w for = {a, b, c} ?
nh du con amt tri nht
Vt qua phiGp # li li
Gp # qua phi
nh du con amt phi nht
Vt qua tri
-
8/10/2019 Ch3_TuringMachine
12/32
thuyt tnh ton -- Ch.3 - My Turing
67/189
DefinitionsDefinitions
The languages accepted by TM
are called Recursively Enumerable (lit k quy)
Recursion is related to recursion in programs
Enumerable means there is some TM that can enumerate the strings in the
language
A language is Recursiveif it is accepted by some TM
that halts on all inputs
We'll usually assume that
TM halt if they enter a final state
68/189
Standard Turing MachineStandard Turing Machine
Deterministic
Infinite tape in both directions
Tape is the input/output file (online)
The machine we described is the standard:
Computing FunctionsComputing Functionswithwith
Turing MachinesTuring Machines
70/189
Domain: Result Region:
A function )(wf has:
D
NotationNotation
R
Dw)(wf
Rwf )(
fcan be TotaL or Partial Function
71/189
A function may have many parameters
The number of parameters is called arity
yxyxf +=),(
Example: Addition function
with 2-arity
72/189
Integer Domain
Unary:
Binary:
Decimal:
11111
101
5
We prefer unary representation:
easier to manipulate with TMs, first
Data representationData representation
n phn
Nh phn
Thp phn
-
8/10/2019 Ch3_TuringMachine
13/32
thuyt tnh ton -- Ch.3 - My Turing
73/189
Definition:
A function is computable if
there is a Turing Machine such that:
f
Initial configuration Final configuration
Dw Domain
0q
w
fq
)(wf
final stateinitial state
For all
74/189
Initial
Configuration
Final
Configuration
A function is computable if
there is a Turing Machine such that:
f
In other words:
Dw DomainFor all
)(0 wfqwq f
75/189
ExampleExample
The function yyf +=),( is computable
Turing Machine:
Input string: yx0 unary
Output string: 0xy unary
yx, are integers
76/189
0
0q
1 1 1 1
x y
1L L
Start
initial state
The 0 is the delimiter that
separates the two numbers
Input representationInput representation
77/189
Finish
Start
Computing FunctionComputing Function
0q
1 1 1 1
x y
1L L
initial state
0
fq
1 1
yx +
L 11
final state
0
78/189
0
fq
1 1
yx +
L 11Finish
final state
The 0 helps when we use
the result for other operations
Computing FunctionComputing Function
-
8/10/2019 Ch3_TuringMachine
14/32
thuyt tnh ton -- Ch.3 - My Turing
79/189
Turing machine for function
yxyxf +=),(
0q 1q 2q 3qL, L,01
L,11
R,
R,10
R,11
4q
R,11
Exercice:Draw TM for inc(x), dec(x), double(x), x*y?Exercice:Draw TM for inc(x), dec(x), double(x), x*y? 80/189
Execution Example (1)
0
0q
1 1 1 1
Time 0
x y
Final Result
0
4q
1 1 1 1
yx +
11= (2)
11=y (2)
81/189
0
0q
1 1Time 0 1 1
0q 1q 2q 3qL, L,01
L,11
R,
R,10
R,11
4q
R,11
Execution Example (2)
82/189
0q 1q 2q 3qL, L,01
L,11
R,
R,10
R,11
4q
R,11
0q
01 11 1Time 1
Execution Example (3)
83/189
0
0q
1 1 1 1Time 2
0q 1q 2q 3qL, L,01
L,11
R,
R,10
R,11
4q
R,11
Execution Example (4)
84/189
1q
1 11 11Time 3
0q 1q 2q 3qL, L,01
L,11
R,
R,10
R,11
4q
R,11
Execution Example (5)
-
8/10/2019 Ch3_TuringMachine
15/32
thuyt tnh ton -- Ch.3 - My Turing
85/189
1q
1 1 1 11Time 4
0q 1q 2q 3qL, L,01
L,11
R,
R,10
R,11
4q
R,11
Execution Example (6)
86/189
1q
1 11 11Time 5
0q 1q 2q 3qL, L,01
L,11
R,
R,10
R,11
4q
R,11
Execution Example (7)
87/189
2q
1 1 1 11Time 6
0q 1q 2q 3qL, L,01
L,11
R,
R,10
R,11
4q
R,11
Execution Example (8)
88/189
3q
1 11 01Time 7
0q 1q 2q 3qL, L,01
L,11
R,
R,10
R,11
4q
R,11
Execution Example (9)
89/189
3q
1 1 1 01Time 8
0q 1q 2q 3qL, L,01
L,11
R,
R,10
R,11
4q
R,11
Execution Example (10)
90/189
3q
1 11 01Time 9
0q 1q 2q 3qL, L,01
L,11
R,
R,10
R,11
4q
R,11
Execution Example (11)
-
8/10/2019 Ch3_TuringMachine
16/32
thuyt tnh ton -- Ch.3 - My Turing
91/189
3q
1 1 1 01Time 10
0q 1q 2q 3qL, L,01
L,11
R,
R,10
R,11
4q
R,11
Execution Example (12)
92/189
3q
1 11 01Time 11
0q 1q 2q 3qL, L,01
L,11
R,
R,10
R,11
4q
R,11
Execution Example (13)
93/189
4q
1 1 1 01Time 12
0q 1q 2q 3qL, L,01
L,11
R,
R,10
R,11
4q
R,11
HALT & accept
Execution Example (14)
94/189
AnotherAnother Example:Example:f(x) = 2xf(x) = 2x (1)(1)
The function f 2)( = is computable
Turing Machine:
Input string: x unary
Output string: xx unary
is integer
95/189
1
fq
1 1
x2
L 11Finish
final state
0q
1 1
x
1LStart
initial state
AnotherAnother Example:Example:f(x) = 2xf(x) = 2x (2)(2)
96/189
TM Pseudocode for f(x) = 2x
Replace every 1 with $
Repeat: Find rightmost $, replace it with 1
Go to right end, insert 1
Find rightmost $, replace it with 1
Go to right end, insert 1
Until no more $ remain
A Tip!A Tip!
-
8/10/2019 Ch3_TuringMachine
17/32
thuyt tnh ton -- Ch.3 - My Turing
97/189
ExampleTM for f(x) = 2x
0q 1q 2q
3q
R,1$
L,1
L,
R$,1 L,11 R,11
R,
0q
1 1
Start
3q
1 11 1
Finish
1. Replace every 1 with $2. Repeat:
Find rightmost $,replace it with 1
Go to right end, insert 13.Until no more $ remain
1. Replace every 1 with $2. Repeat:
Find rightmost $,replace it with 1
Go to right end, insert 13.Until no more $ remain
98/189
T= ; = { 0, 1, # } ; Q = {q1, q2, q3}
P= { q1, 1 1, R, q1,q2, 0 1, L, q3,q1, 0 0, R, q1,q2, 1 0 L, q2,
q1, # #, L, q2,q2, # 1, L, q3 }
TM compute succ(n)TM compute succ(n)
1q 3q
0|0, R
2q
1|1, R1|0, L
#|1, L
#|#, L
0|1, L
99/189
An example: Unary to Binary ConversionAn example: Unary to Binary Conversion
Input string: #aaaaaa#
Output string?
#110###
2q
X|#, L
#|#, L
#|1, R
1|0, L
0|1, R
a|X, L1q0q
1|1, R
0|0, R
Find a
Increment 1
Erase X3q
0|0, R1|1, RX|X, R
X|X, L
Exercice: Draw TM for Inverse?Exercice: Draw TM for Inverse?100/189
Another ExampleAnother ExampleAnother Example
The function iscomputable =),( yf
0
1 yx>
yx
if
if
Turing Machine for
Input: yx0
Output: 1 0or
101/189
Turing Machine Pseudocode:
Match a 1 from with a 1 fromx y
Repeat
Until all of or is matchedy
If a 1 from is not matched
erase tape, write 1
else
erase tape, write 0
x
)( yx>
)( yx
102/189
TM move RWH to the right at nth = 2
q2q1
$ | $, Rq3
x | x, R
y | y, L x | x, Lq4
x|x, Rx \
$ x
Assume that in the Input Tape, we have Input String w1$w2$$wkwi , $
Draw a TM in order to move RWH to the right at w3:
P = { q1, x x, R, q1, vi x
q1, $ $, R, q2, vi $ q2, x x, R, q2, vi x q2, $ $, R, q3, vi y q3, x x, L, q4, vi x }
$
x \
Tng qut : Di RWH v v tr n,vi n t trc cu von$w1$w2$$wk
Tng qut : Di RWH v v tr n,vi n t trc cu von$w1$w2$$wk
-
8/10/2019 Ch3_TuringMachine
18/32
thuyt tnh ton -- Ch.3 - My Turing
103/189
TM move RWH one case to the right
P= { q1, 0 #, R, q(0),q1, 1 #, R, q(1),q(0), 0 0, R, q(0),q(0), 1 0, R, q(1),q(1), 0 1, R, q(0),q(1), 1 1, R, q(1),
q(0), # 0, R, q(#),q(1), # 1, R, q(#),q(#), 0 #, R, q(0),q(#), 1 #, R, q(1)}
q(0)
q1
0 | #, R
0 | 0, R
q(1)1 | #, R
0 | 1, R 1 | 0, R q#
# | 1, R
# | 1, R
1 | 0, R# | 0, R
0 | #, R
1 | 1, R
Remember 0
Remember 1
Restore 0,
to the right
Restore 1
0# # * # # 0 #
1# # * # # 1 #
Or
104/189
CopyG: G* (G*)2 oror ww w#w
0 | 0, R
q2
b | b, R
1 | 1, R 0 | 0, L
q3
b | b, L
1 | 1, L
q4
1 | 1, R
q5
0 | 0, L
q1
q6
# | 1, L
# | #, L
1 | 1, L
0 | 0, R
b | b, R# | 0, L
b | b, L
1 | a, R
0 | b, R
a | 1, R
b | 0 , R
w w#wR?ww w#wR?
Combining Turing MachinesCombining Turing Machines
106/189
Block Diagram
Turing
Machineinput output
107/189
Example:
=),( y
yx + y>if
0 yif
Comparer
Adder
Eraser
yx,
yx,
y>
y
yx +
0
TuringTurings Thesiss Thesis
-
8/10/2019 Ch3_TuringMachine
19/32
thuyt tnh ton -- Ch.3 - My Turing
109/189
Turings thesis*:
Any computation carried outby mechanical means
can be performed by a Turing Machine
(1930)
Thesis: A Accepted Statement but not DemontrationThesis: A Accepted Statement but not Demontration
110/189
Computer Science Law:
A computation is mechanical
if and only if
it can be performed by a Turing Machine
There is no known model of computation
more powerful than Turing Machines
111/189
Definition of Algorithm:
An algorithm for function
is a
Turing Machine which computes
)(wf
)(wf
112/189
When we say:
There exists an algorithm
Algorithms are Turing Machines
We mean:
There exists a Turing Machine
that executes the algorithm
VariationsVariationsofof the Turingthe Turing MachineMachine
114/189
Read-Write Head
Control Unit
Deterministic
The Standard ModelThe Standard Model
Infinite Tape
(Left or Right)
a a c ba cb b a a
-
8/10/2019 Ch3_TuringMachine
20/32
thuyt tnh ton -- Ch.3 - My Turing
115/189
Variations of the Standard ModelVariations of the Standard Model
1. Stay-Option
2. Semi-Infinite Tape3. Off-Line
4. Multitape
5. Multidimensional
6. Nondeterministic
Turing machines with:
116/189
We want to prove:
Each Class has the same powerwith the Standard Model
The variations form different
Turing Machine Classes
Variations of the Standard ModelVariations of the Standard Model
What is the Same Power of two classes?
(or Equivalence)
117/189
For any machine of first class1there is a machine of second class2
such that: )()( 21 LL =
And vice-versa
Same Power of two classes means:
Both classes of Turing machines accept
the same languages
NotionNotion
118/189
a technique to prove same powerSimulation:
Simulate the machine of one class
with a machine of the other class
First Class
Original Machine
1 1
2
Second Class
Simulation Machine
DemonstrationDemonstration
119/189
Configurations in the Original Machine
correspond to configurations
in the Simulation MachineOriginal Machine:
Simulation Machine:
nddd L10
nddd L10
Correspondence of theCorrespondence of the
configurationsconfigurations
120/189
The Simulation Machine
and the Original Machine
accept the same language
fdOriginal Machine:
Simulation Machine:fd
Final Configuration
Correspondence of theCorrespondence of the
configurationsconfigurations
-
8/10/2019 Ch3_TuringMachine
21/32
thuyt tnh ton -- Ch.3 - My Turing
121/189
Turing Machines with StayTuring Machines with Stay--OptionOption
The head can stay in the same position
a a c ba cb b a a
Left, Right, Stay
L,R,S: moves
122/189
Example:
1q 2q
a a c ba cb b a a
Time 1
1q
Sba ,
ba c ba cb b a a
Time 2
2q
123/189
Stay-Option Machines
have the same power with
Standard Turing machines
Theorem:
124/189
Proof of Part 1 :
Part 1: Stay-Option Machines
are at least as powerful as
Standard machines
Proof: a Standard machine is also
a Stay-Option machine
(that never uses the S move)
125/189
Part 2: Standard Machines
are at least as powerful as
Stay-Option machines
Proof: a standard machine can simulate
a Stay-Option machine
Proof of Part 2 :
126/189
1q 2q
Lba ,
Stay-Option Machine
Simulation in Standard Machine
Simulationfor Left moves
Similar for Right moves
1q 2qLba ,
-
8/10/2019 Ch3_TuringMachine
22/32
thuyt tnh ton -- Ch.3 - My Turing
127/189
1q 2qSba ,
Stay-Option Machine
1q 2qLba ,
3qRx,
Simulation in Standard Machine
For every symbol x
Simulationfor Stay
128/189
Example
Stay-Option Machine:
aab a
1q
1 ba b a
2q
21q 2q
Sba ,
Simulation in Standard Machine:
aab a
1q
1 ba b a
2q
2 ba b a
3q
3
129/189
Standard Machine--Multiple Track Tape
b
d
a
bba
a
c
track 1
track 2
one symbol
Turing Machines withTuring Machines with Multiple Track TapeMultiple Track Tape
130/189
1q 2qLdcab ),,(),(
b
d
a
bba
a
c
track 1
track 2
1q
Turing Machines withTuring Machines with Double Track TapeDouble Track Tape
b
d
a
b
c
d
a
c
track 1
track 2
2q
131/189
Turing Machines withTuring Machines with SemiSemi--InfiniteInfinite TapeTape
...# a b a c
132/189
Standard Turing machines
simulate
Semi-infinite tape machines:
Trivial
StandardStandard SemiSemi--InfiniteInfinite TapeTape
-
8/10/2019 Ch3_TuringMachine
23/32
thuyt tnh ton -- Ch.3 - My Turing
133/189
Semi-infinite tape machines simulate
Standard Turing machines:Standard machine
.........
Semi-infinite tape machine
..................
SemiSemi--Infinite TapeInfinite Tape Standard (1)Standard (1)
134/189
Semi-infinite tape machine with two tracks
Standard machine
..................
reference point
a b c d e
.........#
#
Right part
Left part ac b
d e
SemiSemi--Infinite TapeInfinite Tape Standard (2)Standard (2)
135/189
1q2q
Standard machine
Rq2Lq1
Lq2 R
q1
Left part Right part
Semi-infinite tape machine
SemiSemi--Infinite TapeInfinite Tape Standard (3)Standard (3)
136/189
1q 2qRa ,
Standard machine
Lq1
Lq2
Lgxax ),,(),(
Rq1Rq2
Rxxa ),,(),(
Semi-infinite tape machine
Left part
Right part
For all symbols
SemiSemi--Infinite TapeInfinite Tape Standard (4)Standard (4)
137/189
.........
Semi-infinite tape machine
#
#
Right part
Left part ac b
d e
Lq1
Standard machine.................. a b c d e
1q
Time 1
SemiSemi--Infinite TapeInfinite Tape Standard (5)Standard (5)
138/189
SemiSemi--Infinite TapeInfinite Tape Standard (5)Standard (5)
Standard machine.................. b c d e
Time 2
2q
.........
Semi-infinite tape machine
#
#
Right part
Left part cd e
b
Lq2
-
8/10/2019 Ch3_TuringMachine
24/32
thuyt tnh ton -- Ch.3 - My Turing
139/189
At the border:
Lq1Rq1
R),#,(#)#,(#
Semi-infinite tape machine
Left part
Rq1Lq1
R),#,(#)#,(# Right part
140/189
.........
Semi-infinite tape machine
#
#
Right part
Left part c b
d e
Lq1
.........#
#
Right part
Left part c b
d e
Rq1
Time 1
Time 2
141/189
Theorem:
Semi-infinite tape machines
have the same power with
Standard Turing machines
142/189
The OffThe Off--LineLine Machine (MMachine (My vy vi bng ti bng tch rch ri)i)
Control Unit
Input File
Tape
read-only
a b c
d eg
read-write
143/189
Off-line machines simulate
Standard Turing Machines:
Off-line machine:
1. Copy input file to tape
2. Continue computation as in
Standard Turing machine
144/189
1. Copy input file to tape
Input Filea b c
a b c
Standard machine
Off-line machine
Tape
a b c
-
8/10/2019 Ch3_TuringMachine
25/32
-
8/10/2019 Ch3_TuringMachine
26/32
thuyt tnh ton -- Ch.3 - My Turing
151/189
a c e d
2q 2q
Time 2
RLdgfb ,),,(),( 1q 2q
a b c e
1q
1
q
Time 1Tape 1 Tape 2
MultitapeMultitape TMs (2)TMs (2)
152/189
Multitape machines simulate
Standard Machines:
Use just one tape
MultitapeMultitape StandardStandard
153/189
Standard machines simulate
Multitape machines:
Use a multi-track tape
A tape of the Multiple tape machine
corresponds to a pair of tracks
Standard machine:
StandardStandard MultitapeMultitape
154/189
a b c h e g
Multitape Machine
Tape 1 Tape 2
Standard machine with four track tape
a b c
e f0 0
0 0
1
1
Tape 1
head position
Tape 2
head position
h
0
155/189
Repeat for each state transition:
Return to reference point
Find current symbol in Tape 1
Find current symbol in Tape 2
Make transition
a b c
e f g0 0
0 0
1
1
Tape 1
head position
Tape 2
head position
h
0
##
#
#
Reference point
156/189
Theorem:
Multi-tape machineshave the same power with
Standard Turing Machines
-
8/10/2019 Ch3_TuringMachine
27/32
thuyt tnh ton -- Ch.3 - My Turing
157/189
Same power doesnt imply same speed:
Language }{ nnbaL=
Acceptance Time
Standard machine
Two-tape machine
2n
n
158/189
}{ nnbaL=
Standard machine:
Go back and forth times2n
Two-tape machine:
Copy to tape 2nb
Leave on tape 1na
Compare tape 1 and tape 2
n( steps)
n( steps)
n( steps)
159/189
MultiDimensionalMultiDimensional Turing MachinesTuring Machines
Two-dimensional tape
Position: +2, -1
MOVES: L,R,U,D
U: up D: down
x
y
a
b
c
HEAD
160/189
Multidimensional machines simulate
Standard machines:
Use one dimension
161/189
Standard machines simulate
Multidimensional machines:
Standard machine:
Use a two track tape
Store symbols in track 1
Store coordinates in track 2
162/189
Two-dimensional machine
x
y
a
b
c
1q
1q
Standard Machine
a
1b
#
symbols
coordinates1 # 2# 1c
#
-
8/10/2019 Ch3_TuringMachine
28/32
thuyt tnh ton -- Ch.3 - My Turing
163/189
Repeat for each transition
Update current symbol Compute coordinates of next position
Go to new position
Standard machine:
164/189
MultiDimensional Machines
have the same powerwith Standard Turing Machines
Theorem:
165/189
NonDeterministicNonDeterministic Turing MachinesTuring Machines
Lba ,
Rca ,
1q
2q
3q
Non Deterministic Choice
166/189
Lba ,
Rca ,
1q
2q
3q
a b c
1q
Time 0
Time 1
b b c
2q
Choice 1
c b c
3q
Choice 2
NondeterministicNondeterministic choiceschoices
167/189
Input string is accepted if
this a possible computationw
Initial configuration Final Configuration
Final state
NotionNotion
yqxwq0 f
168/189
NonDeterministic Machines simulate
Standard (deterministic) Machines:
Every deterministic machine
is also a nondeterministic machine
-
8/10/2019 Ch3_TuringMachine
29/32
thuyt tnh ton -- Ch.3 - My Turing
169/189
Deterministic machines simulate
NonDeterministic machines:
Keeps track of all possible computations
Deterministic machine:
170/189
Non-Deterministic Choices
Computation 1
1q
2q
4q
3q
5q
6q 7q
Computation 2
1q
2q
4q
3q
5q
6q 7q
171/189
Keeps track of all possible computations
Deterministic machine:
Simulation
Stores computations in a
two-dimensional tape
172/189
Lba ,
Rca ,
1q
2q
3q
a b c
1q
Time 0
NonDeterministic machine
Deterministic machine
a b c
1q
# # # # ####
# # #
##
# #
Computation 1
173/189
NonDeterministic machine
b b c
2q
# # # # #### #
#
# #
Computation 1
c b c3q ## Computation 2
Deterministic machine
b b c
2q
Choice 1
c b c
3qChoice 2
Time 1Lba ,
Rca ,
1q
2q
3q
174/189
Repeat
Execute a step in each computation:
If there are two or more choices
in current computation:
1. Replicate configuration
2. Change the state in the replica
-
8/10/2019 Ch3_TuringMachine
30/32
thuyt tnh ton -- Ch.3 - My Turing
175/189
Theorem: NonDeterministic Machines
have the same power withDeterministic machines
176/189
Remark:
The simulation
in the Deterministic machinetakes time exponential time compared
to the NonDeterministic machine
A Universal Turing MachineA Universal Turing Machine
178/189
Turing Machines are hardwired
they execute
only one program
A limitation of Turing Machines:
Real Computers are re-programmable
Solution: Universal Turing Machine(My Turing vn nng, hay phqut)
Reprogrammable machine Simulates any other Turing Machine
Attributes:
179/189
Universal Turing Machine
Simulates any other Turing Machine M
Input of Universal Turing Machine:
Description of transitions of M
Initial tape contents of M
180/189
UniversalTuring
Machine
Three tapes
Description of M
Tape 1
Tape contents of M
Tape 2
State of M
Tape 3
Universal Turing Machine
-
8/10/2019 Ch3_TuringMachine
31/32
thuyt tnh ton -- Ch.3 - My Turing
181/189
We describe TM Mas a string of symbols: We encode Mas a string of symbols
Alphabet Encoding
Description of M
Tape 1
Symbols: a b c d K
Encoding: 1 11 111 1111
Encoding in Tape 1
182/189
State Encoding
States: 1q 2q 3q 4q K
Encoding: 1 11 111 1111
Encoding in Tape 1
Description of M
Tape 1
183/189
Head Move Encoding
Move:
Encoding:
L R
1 11
Encoding in Tape 1
Description of M
Tape 1
184/189
Encoding in Tape 1
Transition Encoding
Transition: ),,(),( 21 Lbqaq =
Encoding: 10110110101
Using 0 for Separator
Description of M
Tape 1
185/189
Machine Encoding
Transitions:
),,(),( 21 Lbqaq =
Encoding:
),,(),( 32 Rcqbq =
10110110101 110111011110101100
Encoding in Tape 1
Description of M
Tape 1
Using 00 for Transition Separator186/189
Universal Turing Machine
Tape 1 contents of Universal TM M:
encoding of the simulated machineas a binary string of 0s and 1s
A TM is describedwith a binary string of 0s and 1s
-
8/10/2019 Ch3_TuringMachine
32/32
187/189
Language of Turing Machines
L = { 010100101, 00100100101111, 111010011110010101, }
TM 1, TM 2, . . .
The set of Turing machines forms a language,
therefore :
each string of the language
is the binary encoding of a TM
188/189
Question: How powerful is RAM?
Can RAMs compute functions/languages that TMscannot?
Can RAMs compute functions/languages significantlyfaster then TMs?
Theorem:A TMA TM can simulate a RAM, provided that the elementary RAMcan simulate a RAM, provided that the elementary RAMinstructions can themselves be simulated by a TMinstructions can themselves be simulated by a TM
189/189
ProofProof
The idea is to use a multitape TM MThe idea is to use a multitape TM Mto perform simulation:to perform simulation:
Tape 1:Tape 1:keeps programkeeps program instructionsinstructions vvii in binary, numbered, and separated byin binary, numbered, and separated by
## andand **, i.e.,, i.e.,
#0*v#0*v00#1*v#1*v11#10*v#10*v22###i*v#i*vii##
Tape 2:Tape 2: keeps data registers:keeps data registers:#0*r#0*r00#1*r#1*r11#10*r#10*r22###i*r#i*rii##
Tape 3:Tape 3: keeps program counterkeeps program counter
Tape 4:Tape 4: keeps data address registerkeeps data address register