Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 7: Turning Machines 虞台文 大同大學資工所 智慧型多媒體研究室.

Similar presentations


Presentation on theme: "Lecture 7: Turning Machines 虞台文 大同大學資工所 智慧型多媒體研究室."— Presentation transcript:

1 Lecture 7: Turning Machines 虞台文 大同大學資工所 智慧型多媒體研究室

2 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

3 Lecture 7: Turning Machines An Overview on Finite State Machine 大同大學資工所 智慧型多媒體研究室

4 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. – 0011001001(legal) – 11001001001(illegal) Writing a C program to do so.

5 q0q0 q1q1 q2q2 q3q3 0 0 0 0 1111 Examples: 00100011 01101011 001010101 0010010101 Finite State Machine

6 q0q0 q1q1 q2q2 q3q3 0 0 0 0 1111 state q0q0 q1q1 q2q2 q3q3 symbol (event) 01 q1q1 q0q0 q3q3 q2q2 q2q2 q3q3 q0q0 q1q1 ok err The Parser

7 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

8 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;

9 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

10 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

11 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

12 Implementation (II) q0q0 q1q1 q2q2 q3q3 0 0 0 0 1111 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

13 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();

14 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; }

15 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; }

16 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 };

17 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

18 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.

19 Lecture 7: Turning Machines The Definition of Turing Machine 大同大學資工所 智慧型多媒體研究室

20 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

21 The Transition Function 1.Change state from q to p. 2.b   print b ; b  {L, R}  Move head in the direction of b.

22 The Transition Function K  ....... a..... q

23 Memory Configuration wa # Head u  # or The configuration of a Turing machine is a member of

24 Halt Configuration wa # Head u  # or The configuration of a Turing machine is a member of

25 Lecture 7: Turning Machines Computing with Turing Machine 大同大學資工所 智慧型多媒體研究室

26 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.

27 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

28 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.

29 Turing Computable Functions w Head ## u ##

30 Example Head #111111111# # Y #

31 Example Head #111111111# # N # 1

32 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 /

33 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.

34 Discussion Three main usages of machines: – Compute – Decision – Accept See textbooks for the definitions of – Turing decidability – Turing acceptability

35 Lecture 7: Turning Machines Turing-Machine Programming 大同大學資工所 智慧型多媒體研究室

36 Simplified Notation >L>L#L#L 1 #L#L 1 # 1 RYR # RNR # # 0 0 00 0

37 Basic Turing-Machines 1. |  | symbol-writing machines: 2. Head-moving machines q0q0 h > a/a/ q0q0 h > L/L/ q0q0 h > R/R/

38 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

39 Lecture 7: Turning Machines Some Example of Powerful TMs 大同大學資工所 智慧型多媒體研究室

40 Some Powerful TMs >R >L 1. 2. 3. 4.

41 Abbreviation >R R a b c # a, b, c, # >RR

42 Example: (Copier) ├ Head #abc# # # abcabc#

43 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# #

44 Example: (Shift-Left) ├ Head #abc# abc#

45 Example: (Shift-Left) ├ # a b c # a a b c # a b b c # a a b c # … >L # R LsR L# #

46 Example: (Palindrome) ├ Head #abb# a Y ##

47 Example: (Palindrome) ├ Head #aba# a N ##

48 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 # #

49 Exercises 3. 4.

50 Exercises 5. 6. where w and v are strings of decimal numbers such that

51 Lecture 7: Turning Machines Extensions of the TM 大同大學資工所 智慧型多媒體研究室

52 Extensions of the TM Standard TM 2-way TM k-tape (1-way) TM Nondeterministic TM 1-way TM Deterministic TM’s

53 Two-Way Infinite Tape wau Head Memory Configuration

54 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.

55 # Proof simulates 33 22 11 0123  $ 0 11 1 22 2 33 3 44 ## M1M1 M2M2

56 Proof simulates

57 Proof simulates E.g.,

58 Proof simulates Mark left end Final Operations Head on lower track Head on upper track Mark halt configuration

59 Proof simulates E.g.,

60 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

61 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

62 Proof 1011001 M1M1 # Head 01234 11 22 $ 1 0 M2M2 1 1 0 # 0 # 1 # # 1. Simulate M 1 on upper track: Simulate the operations of M 1 on M 2. Determine

63 Proof $ 1 0 M2M2 1 1 0 1 0 # 1 # Head # 2. Simulate M 1 on lower track: Simulate the operations of M 1 on M 2. Determine 1011001 M1M1 # Head 01234 11 22 1 33

64 Proof $ 1 0 M2M2 1 1 0 1 0 # 1 # Head # 3. Change track: Simulate the operations of M 1 on M 2. Determine $ 1 0 M2M2 1 1 0 1 0 # 1 # Head #

65 Proof 4. Extend the tape to the right: Simulate the operations of M 1 on M 2. Determine $ 1 0 M2M2 1 1 0 1 0 # 1 # Head # $ 1 0 M2M2 1 1 0 1 0 # 1 # #

66 Proof 4. Extend the tape to the right: Simulate the operations of M 1 on M 2. Determine $ 1 0 M2M2 1 1 0 1 0 # 1 # Head # $ 1 0 M2M2 1 1 0 1 0 # 1 # # # # # #

67 Proof 5. Halt and record head position: Simulate the operations of M 1 on M 2. Determine $ 1 0 M2M2 1 1 0 1 0 # 1 # Head # $ 1 0 M2M2 1 1 0 1 0 # 1 # #

68 Proof 5. Halt and record head position: Simulate the operations of M 1 on M 2. Determine $ 1 0 M2M2 1 1 0 1 0 # 1 # Head # $ 1 0 M2M2 1 1 0 1 0 # 1 # #

69 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

70 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 M2M2 1 1 0 1 0 # 1 # # # M2M2 # 11011001

71 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.

72 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.

73 k-Tape TM Usually for I/O Usually for working memory Head                                       ... Memory Configuration

74 Example (Duplicate) w## # w## # w## #w# w## #w# w## #w# w#

75 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.

76 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.

77 Lecture 7: Turning Machines Nondeterministic Turing Machine 大同大學資工所 智慧型多媒體研究室

78 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.

79 Transition Function E.g., K  ....... a..... q p1p1 p2p2 prpr a a a q

80 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.

81 Example a regular language Head #  ab# ba b Head #  ab# aa a There is a terminating path. There is no a terminating path.

82 Example a regular language a, b babba # #b, #a, # b, #

83 Example n is a composite number iff

84 Example L can be accepted by the NTM >GGPE Generate p  2 Generate q  2 Compute m = pq Compare m and n

85 Example L can be accepted by the NTM >GGPE #IIIIIIIIII# Head n = 10

86 Example L can be accepted by the NTM >GGPE #IIIIIIIIII# Head n = 10 G : generate a random number larger than 2. >RIR IR# # #

87 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# # #

88 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# # #

89 Example L can be accepted by the NTM >GGPE #IIIIIIIIII# Head n = 10 P : product II#IIIII# p = 2 q = 5

90 Example L can be accepted by the NTM >GGPE #IIIIIIIIII# Head n = 10 P : product II# m = pq = 10 IIIIIIII

91 Example L can be accepted by the NTM >GGPE #IIIIIIIIII# Head n = 10 P : product II# m = pq = 10 IIIIIIII

92 Example L can be accepted by the NTM >GGPE #IIIIIIIIII# Head n = 10 E : equivalence II# m = pq = 10 IIIIIIII

93 Example L can be accepted by the NTM >GGPE #IIIIIIIIII# Head n = 10 II# m = pq = 10 IIIIIIII n is a composite number iff

94 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.

95 Proof K1K1 11 ....... a..... q Numbering: p1p1 p2p2 p3p3 a a a q 1 2 3, 4, …,10

96 Proof Computation path indexing > d1d1 d2d2 d3d3 d1d1 d2d2 d1d1 d2d2 d3d3 d4d4

97 Proof An computation path ( #d 2 d 1 d 4 # ) > d1d1 d2d2 d3d3 d1d1 d2d2 d1d1 d2d2 d3d3 d4d4

98 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# … … … … …

99 Proof #w Head # 1st tape: never changed. 2nd tape: simulate the computation of M 1. 3rd tape: computation path. #w Head # $ w # # 2 M #

100 Proof Copy tape1 to tape 2. >$ (2) R (2) R (3) C M 1 ’ G # (3)

101 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

102

103 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.


Download ppt "Lecture 7: Turning Machines 虞台文 大同大學資工所 智慧型多媒體研究室."

Similar presentations


Ads by Google