Lecture 7: Turning Machines 虞台文 大同大學資工所 智慧型多媒體研究室
Content An Overview on Finite State Machine The Definition of Turing Machine Computing with Turing Machine Turing-Machine Programming Some Examples of Powerful TMs Extensions of the TM Nondeterministic Turing Machine
Lecture 7: Turning Machines An Overview on Finite State Machine 大同大學資工所 智慧型多媒體研究室
Example Input a 0/1 sting. If the numbers of 0 and 1 in the string are both even, then it is legal; otherwise, it is illegal. – (legal) – (illegal) Writing a C program to do so.
q0q0 q1q1 q2q2 q3q Examples: Finite State Machine
q0q0 q1q1 q2q2 q3q state q0q0 q1q1 q2q2 q3q3 symbol (event) 01 q1q1 q0q0 q3q3 q2q2 q2q2 q3q3 q0q0 q1q1 ok err The Parser
Implementation (I) state q0q0 q1q1 q2q2 q3q3 symbol (event) 01 q1q1 q0q0 q3q3 q2q2 q2q2 q3q3 q0q0 q1q1 ok err The Parser #defineq00 #defineq11 #defineq22 #defineq33 #definefini4 #defineq00 #defineq11 #defineq22 #defineq33 #definefini4
Implementation (I) state q0q0 q1q1 q2q2 q3q3 symbol (event) 01 q1q1 q0q0 q3q3 q2q2 q2q2 q3q3 q0q0 q1q1 ok err The Parser int parser[4][3]={ q1, q2,fini, q0,q3,fini, q3,q0,fini, q2,q1,fini }; intstate=q0; intevent; char* str; int parser[4][3]={ q1, q2,fini, q0,q3,fini, q3,q0,fini, q2,q1,fini }; intstate=q0; intevent; char* str;
Implementation (I) void ToEvent(char c) { if(c == ’0’) event = 0; else if(c == ’1’) event = 1; else event = 2; } void ToEvent(char c) { if(c == ’0’) event = 0; else if(c == ’1’) event = 1; else event = 2; } Event (or Message) Encoding
Implementation (I) void main() { // Ask user to input a 0/1 string // Store the string into str state = q0; //initialization while(state!=fini){ ToEvent(*str++); EventHandler(event); } void main() { // Ask user to input a 0/1 string // Store the string into str state = q0; //initialization while(state!=fini){ ToEvent(*str++); EventHandler(event); } } Event (or Message) Loop
Implementation (I) void EventHandler(int event) { int next_state = parser[state][event]; switch(next_state){ case fini: printf(”%s\n”, state==q0 ? ”ok” : ”err”); default: state = next_state; //change state } void EventHandler(int event) { int next_state = parser[state][event]; switch(next_state){ case fini: printf(”%s\n”, state==q0 ? ”ok” : ”err”); default: state = next_state; //change state } } Event Handler
Implementation (II) q0q0 q1q1 q2q2 q3q state q0q0 q1q1 q2q2 q3q3 symbol (event) 01 pq 1 pq 0 pq 3 pq 2 pq 3 pq 0 pq 1 ok err The Parser
Implementation (II) state q0q0 q1q1 q2q2 q3q3 symbol (event) 01 pq 1 pq 0 pq 3 pq 2 pq 3 pq 0 pq 1 ok err The Parser #defineq00 #defineq11 #defineq22 #defineq33 #definefini4 #defineq00 #defineq11 #defineq22 #defineq33 #definefini4 void pq0(), pq1(), pq2(), pq3(); void ok(), err(); void pq0(), pq1(), pq2(), pq3(); void ok(), err();
Implementation (II) state q0q0 q1q1 q2q2 q3q3 symbol (event) 01 pq 1 pq 0 pq 3 pq 2 pq 3 pq 0 pq 1 ok err The Parser void pq0() { state = q0; } void pq1() { state = q1; } void pq0() { state = q0; } void pq1() { state = q1; } void pq2() { state = q2; } void pq3() { state = q3; } void pq2() { state = q2; } void pq3() { state = q3; }
Implementation (II) state q0q0 q1q1 q2q2 q3q3 symbol (event) 01 pq 1 pq 0 pq 3 pq 2 pq 3 pq 0 pq 1 ok err The Parser void ok() { printf(”ok\n”); state = fini; } void err() { printf(”error\n”); state = fini; } void ok() { printf(”ok\n”); state = fini; } void err() { printf(”error\n”); state = fini; }
Implementation (II) state q0q0 q1q1 q2q2 q3q3 symbol (event) 01 pq 1 pq 0 pq 3 pq 2 pq 3 pq 0 pq 1 ok err The Parser typedef void (*FUNCTION)(); FUNCTION parser[4][3]={ pq1, pq2, ok, pq0, pq3, err, pq3, pq0, err, pq2, pq1, err }; typedef void (*FUNCTION)(); FUNCTION parser[4][3]={ pq1, pq2, ok, pq0, pq3, err, pq3, pq0, err, pq2, pq1, err };
Implementation (II) void main() { // Ask user to input a 0/1 string // Store the string into str state = q0; //initialization while(state!=fini){ ToEvent(*str++); (*parser[state][event])(); } void main() { // Ask user to input a 0/1 string // Store the string into str state = q0; //initialization while(state!=fini){ ToEvent(*str++); (*parser[state][event])(); } } Event (or Message) Loop
Exercise 1. Write a C Program to recognize floating- point string. The syntax of the floating- point string is defined the same as that defined in C language.
Lecture 7: Turning Machines The Definition of Turing Machine 大同大學資工所 智慧型多媒體研究室
Definition # # Head A Turing machine is a quadruple K : finite set of states, h K. : alphabet, # , L, R . s : s K, initial state. : transition function Hang
The Transition Function 1.Change state from q to p. 2.b print b ; b {L, R} Move head in the direction of b.
The Transition Function K a..... q
Memory Configuration wa # Head u # or The configuration of a Turing machine is a member of
Halt Configuration wa # Head u # or The configuration of a Turing machine is a member of
Lecture 7: Turning Machines Computing with Turing Machine 大同大學資工所 智慧型多媒體研究室
Yields in One Step ├ M Let Then, ├M├M if and only if, where such that w1w1 a1a1 u1u1 w2=w1w2=w1 a2a2 u2=u1u2=u1 w1=w2a2w1=w2a2 a1a1 u1u1 w2w2 a2a2 u2=a1u1u2=a1u1 w1w1 a1a1 u1=a2u2u1=a2u2 w2=w1a1w2=w1a1 a2a2 u2u2 Used to trace the computation sequence.
Yields ├ where C i denotes the configuration of M. We say that computation sequence (1) is of length n or has n steps. Let is the reflexive, transitive closure of ├ M, i.e., if, for some n 0, ├ ├M├M ├ (1) ├M├M ├M├M
Turing Computable Functions Let 0, 1 {#}, and let. f is said to be a Turing computable function if such that, for any, ├ M is, then, said to compute f.
Turing Computable Functions w Head ## u ##
Example Head # # # Y #
Example Head # # # N # 1
Example q0q0 q1q1 q 11 q2q2 q 21 q3q3 q 31 q7q7 q6q6 q4q4 q5q5 h > L /# / 1L /# / 1L /# / 1 R / # Y / R / # N / R / 0 / 0 L /
Exercise q0q0 q1q1 q 11 q2q2 q 21 q3q3 q 31 q7q7 q6q6 q4q4 q5q5 h > L /# / 1L /# / 1L /# / 1 R / # Y / R / # N / R / 0 / 0 L / 2.Write the state transition table.
Discussion Three main usages of machines: – Compute – Decision – Accept See textbooks for the definitions of – Turing decidability – Turing acceptability
Lecture 7: Turning Machines Turing-Machine Programming 大同大學資工所 智慧型多媒體研究室
Simplified Notation >L>L#L#L 1 #L#L 1 # 1 RYR # RNR # #
Basic Turing-Machines 1. | | symbol-writing machines: 2. Head-moving machines q0q0 h > a/a/ q0q0 h > L/L/ q0q0 h > R/R/
Combining Rules >M 1 >M 2 >M 3 q 10 h ?/? q1mq1m > q 30 h > ?/? q3pq3p 1. >M 1 M 2 >M 1 M 2 q 20 h ?/? q2nq2n q 10 ?/? q1mq1m > q 20 h ?/? q2nq2n > 2. a b q 10 q1hq1h ?/? q1mq1m > q 20 h ?/? q2nq2n a/a q 30 ?/? q3pq3p b/b
Lecture 7: Turning Machines Some Example of Powerful TMs 大同大學資工所 智慧型多媒體研究室
Some Powerful TMs >R >L
Abbreviation >R R a b c # a, b, c, # >RR
Example: (Copier) ├ Head #abc# # # abcabc#
Example: (Copier) ├ # a b c # # # b c # # # b c # a # a b c # a # # b c # # >L # R #R # R # sL # L # s R#R# #
Example: (Shift-Left) ├ Head #abc# abc#
Example: (Shift-Left) ├ # a b c # a a b c # a b b c # a a b c # … >L # R LsR L# #
Example: (Palindrome) ├ Head #abb# a Y ##
Example: (Palindrome) ├ Head #aba# a N ##
Example: (Palindrome) ├ # a b b a # # # a b b a # a # a b b a # a # # b b a # a # # b b # # a # a b b a # … (S R ) >S R L # LaRR#R # L#L # L # #RYR L # #RNR #L # #
Exercises 3. 4.
Exercises where w and v are strings of decimal numbers such that
Lecture 7: Turning Machines Extensions of the TM 大同大學資工所 智慧型多媒體研究室
Extensions of the TM Standard TM 2-way TM k-tape (1-way) TM Nondeterministic TM 1-way TM Deterministic TM’s
Two-Way Infinite Tape wau Head Memory Configuration
Lemma a two-way TM one-way TM that simulates M 1 as follows: 1. ├ ├ 2. M 1 does not halt M 2 does not halt.
# Proof simulates 33 22 1 $ 0 11 1 22 2 33 3 44 ## M1M1 M2M2
Proof simulates
Proof simulates E.g.,
Proof simulates Mark left end Final Operations Head on lower track Head on upper track Mark halt configuration
Proof simulates E.g.,
Proof 1.M 2 simulates the input for M 1 into M 2 in the following way: abcbbcb M1M1 # Head $ a # M2M2 b # c # b # b # c # b # # # g s2s2
Proof 1.M 2 simulates the input for M 1 into M 2 in the following way: 2.Simulate the operations of M 1 on M 2. 3.When M 2 would halt, restore the tape to “single track” format by M 1. abcbbcb M1M1 # Head $ a # M2M2 b # c # b # b # c # b # # # g
Proof M1M1 # Head 11 22 $ 1 0 M2M # 0 # 1 # # 1. Simulate M 1 on upper track: Simulate the operations of M 1 on M 2. Determine
Proof $ 1 0 M2M # 1 # Head # 2. Simulate M 1 on lower track: Simulate the operations of M 1 on M 2. Determine M1M1 # Head 11 22 1 33
Proof $ 1 0 M2M # 1 # Head # 3. Change track: Simulate the operations of M 1 on M 2. Determine $ 1 0 M2M # 1 # Head #
Proof 4. Extend the tape to the right: Simulate the operations of M 1 on M 2. Determine $ 1 0 M2M # 1 # Head # $ 1 0 M2M # 1 # #
Proof 4. Extend the tape to the right: Simulate the operations of M 1 on M 2. Determine $ 1 0 M2M # 1 # Head # $ 1 0 M2M # 1 # # # # # #
Proof 5. Halt and record head position: Simulate the operations of M 1 on M 2. Determine $ 1 0 M2M # 1 # Head # $ 1 0 M2M # 1 # #
Proof 5. Halt and record head position: Simulate the operations of M 1 on M 2. Determine $ 1 0 M2M # 1 # Head # $ 1 0 M2M # 1 # #
Proof 1.M 2 simulates the input for M 1 into M 2 in the following way: 2.Simulate the operations of M 1 on M 2. 3.When M 2 would halt, restore the tape to “single track” format by M 1. abcbbcb M1M1 # Head $ a # M2M2 b # c # b # b # c # b # # # g
Proof 1.M 2 simulates the input for M 1 into M 2 in the following way: 2.Simulate the operations of M 1 on M 2. 3.When M 2 would halt, restore the tape to “single track” format by M 1. abcbbcb M1M1 # Head $ a # M2M2 b # c # b # b # c # b # # # g $ 1 0 M2M # 1 # # # M2M2 #
Lemma a two-way TM one-way TM that simulates M 1 as follows: 1. ├ ├ 2. M 1 does not halt M 2 does not halt.
Theorem Any function that is computed or language that is decided or accepted by a two-way Turing Machine is also computed, decided or accepted by a standard Turing Machine.
k-Tape TM Usually for I/O Usually for working memory Head ... Memory Configuration
Example (Duplicate) w## # w## # w## #w# w## #w# w## #w# w#
Lemma M 1 : k -tape TM, k > 0 : alphabet of M 1 s 1 : initial state Standard TM such that 1. M 1 halts on input w, i.e., ├ Then, for M 2, ├ 2. M 1 hangs on w M 2 too. 3. M 1 neither halts nor hangs on w M 2 too.
Theorem Any function that is computed or language that is decided or accepted by a k -tape Turing machine is also computed, decided or accepted by a standard Turing machine.
Lecture 7: Turning Machines Nondeterministic Turing Machine 大同大學資工所 智慧型多媒體研究室
NTM Definition A Turing machine is a quadruple K : finite set of states, h K. : alphabet, # , L, R . s : s K, initial state. : transition function The same as standard TM.
Transition Function E.g., K a..... q p1p1 p2p2 prpr a a a q
Language Acceptance by an NTM NTM is usually used as a language acceptor. A language, say, L can be accepted by an NTM if, given any input w L, there exists a computation sequence which can lead the NTM to a halt state.
Example a regular language Head # ab# ba b Head # ab# aa a There is a terminating path. There is no a terminating path.
Example a regular language a, b babba # #b, #a, # b, #
Example n is a composite number iff
Example L can be accepted by the NTM >GGPE Generate p 2 Generate q 2 Compute m = pq Compare m and n
Example L can be accepted by the NTM >GGPE #IIIIIIIIII# Head n = 10
Example L can be accepted by the NTM >GGPE #IIIIIIIIII# Head n = 10 G : generate a random number larger than 2. >RIR IR# # #
Example L can be accepted by the NTM >GGPE #IIIIIIIIII# Head n = 10 G : generate a random number larger than 2. II# p = 2 >RIR IR# # #
Example L can be accepted by the NTM >GGPE #IIIIIIIIII# Head n = 10 G : generate a random number larger than 2. II#IIIII# p = 2 q = 5 >RIR IR# # #
Example L can be accepted by the NTM >GGPE #IIIIIIIIII# Head n = 10 P : product II#IIIII# p = 2 q = 5
Example L can be accepted by the NTM >GGPE #IIIIIIIIII# Head n = 10 P : product II# m = pq = 10 IIIIIIII
Example L can be accepted by the NTM >GGPE #IIIIIIIIII# Head n = 10 P : product II# m = pq = 10 IIIIIIII
Example L can be accepted by the NTM >GGPE #IIIIIIIIII# Head n = 10 E : equivalence II# m = pq = 10 IIIIIIII
Example L can be accepted by the NTM >GGPE #IIIIIIIIII# Head n = 10 II# m = pq = 10 IIIIIIII n is a composite number iff
Lemma For every NTM, we can construct a standard TM such that for any 1.if M 1 halts on w, then M 2 is also so; 2.if M 1 does not halts on w, then M 2 is also not.
Proof K1K1 11 a..... q Numbering: p1p1 p2p2 p3p3 a a a q 1 2 3, 4, …,10
Proof Computation path indexing > d1d1 d2d2 d3d3 d1d1 d2d2 d1d1 d2d2 d3d3 d4d4
Proof An computation path ( #d 2 d 1 d 4 # ) > d1d1 d2d2 d3d3 d1d1 d2d2 d1d1 d2d2 d3d3 d4d4
Proof G : computation path generation ##d1##d1# #d2##d2# #dr##dr# … #d1d1##d1d1# #d1d2##d1d2# #d1dr##d1dr# … #drdr##drdr# … #d2d1##d2d1# #d1d1d1##d1d1d1# #d1d1d2##d1d1d2# #d1drdr##d1drdr# … #drdrdr##drdrdr# … #d2d1d1##d2d1d1# #d1d1d1d1##d1d1d1d1# #d1d1d1d2##d1d1d1d2# #d1drdrdr##d1drdrdr# … #drdrdrdr##drdrdrdr# … #d2d1d1d1##d2d1d1d1# … … … … …
Proof #w Head # 1st tape: never changed. 2nd tape: simulate the computation of M 1. 3rd tape: computation path. #w Head # $ w # # 2 M #
Proof Copy tape1 to tape 2. >$ (2) R (2) R (3) C M 1 ’ G # (3)
Proof >$ (2) R (2) R (3) C M 1 ’ G # (3) Copy tape1 to tape 2. Simulate M 1 on tape 2 according to the computation path depicted on tape 3. Generate computation path. halt
Lemma For every NTM, we can construct a standard TM such that for any 1.if M 1 halts on w, then M 2 is also so; 2.if M 1 does not halts on w, then M 2 is also not.