1.1k likes | 1.42k Views
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.
E N D
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 botheven, then it is legal; otherwise, it is illegal. • 0011001001(legal) • 11001001001(illegal) • Writing a C program to do so.
0 q0 q1 0 1 1 1 1 0 q2 q3 0 Finite State Machine Examples: 00100011<CR> 01101011<CR> 001010101<CR> 0010010101<CR>
symbol (event) 0 0 1 <CR> q0 q1 0 q0 1 1 1 1 q1 0 state q2 q2 q3 0 q3 Finite State Machine q1 q2 ok q0 q3 err q3 q0 err err q2 q1 The Parser
symbol (event) 0 1 <CR> q0 q1 q2 ok q1 q0 q3 err state q2 q3 q0 err err q3 q2 q1 The Parser Implementation (I) #define q0 0 #define q1 1 #define q2 2 #define q3 3 #define fini 4
symbol (event) 0 1 <CR> q0 q1 q2 ok q1 q0 q3 err state q2 q3 q0 err err q3 q2 q1 The Parser Implementation (I) int parser[4][3]={ q1, q2, fini, q0, q3, fini, q3, q0, fini, q2, q1, fini }; int state=q0; int event; char* str;
Implementation (I) 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); } } 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 } } Event Handler
symbol (event) 0 0 1 <CR> q0 q1 0 q0 1 1 1 1 q1 0 state q2 q2 q3 0 q3 Implementation (II) pq1 pq2 ok pq0 pq3 err pq3 pq0 err err pq2 pq1 The Parser
symbol (event) 0 1 <CR> q0 pq1 pq2 ok q1 pq0 pq3 err state q2 pq3 pq0 err err q3 pq2 pq1 The Parser Implementation (II) #define q0 0 #define q1 1 #define q2 2 #define q3 3 #define fini 4 void pq0(), pq1(), pq2(), pq3(); void ok(), err();
symbol (event) 0 1 <CR> q0 pq1 pq2 ok q1 pq0 pq3 err state q2 pq3 pq0 err err q3 pq2 pq1 The Parser Implementation (II) void pq0() { state = q0; } void pq1() { state = q1; } void pq2() { state = q2; } void pq3() { state = q3; }
symbol (event) 0 1 <CR> q0 pq1 pq2 ok q1 pq0 pq3 err state q2 pq3 pq0 err err q3 pq2 pq1 The Parser Implementation (II) void ok() { printf(”ok\n”); state = fini; } void err() { printf(”error\n”); state = fini; }
symbol (event) 0 1 <CR> q0 pq1 pq2 ok q1 pq0 pq3 err state q2 pq3 pq0 err err q3 pq2 pq1 The Parser typedef void (*FUNCTION)(); FUNCTION parser[4][3]={ pq1, pq2, ok, pq0, pq3, err, pq3, pq0, err, pq2, pq1, err }; Implementation (II)
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])(); } } Event (or Message) Loop
Exercise • 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 大同大學資工所 智慧型多媒體研究室
Hang Head # # Definition A Turing machine is a quadruple K: finite set of states, hK. : alphabet, #, L, R. : transition function s: sK, initial state.
The Transition Function • Change state from q to p. • b printb; • b{L, R} Move head in the direction of b.
a . . . . . . . . . . . . . . . . . . . q K . . . . . The Transition Function
Head # w a u # Memory Configuration or The configuration of a Turing machine is a member of
Head # w a u # Halt Configuration or The configuration of a Turing machine is a member of
Lecture 7: Turning Machines Computing with Turing Machine 大同大學資工所 智慧型多媒體研究室
├M w1 a1 u1 w1=w2a2 a1 u1 w1 a1 u1=a2u2 w2=w1 a2 u2=u1 w2 a2 u2=a1u1 w2=w1a1 a2 u2 Used to trace the computation sequence. Yields in One Step├M Let Then, if and only if , where such that
├ Let is the reflexive, transitive closure of ├M , i.e., if, for some n 0, ├ ├ ├M ├M ├M (1) Yields where Ci denotes the configuration of M. We say that computation sequence (1) is of length n or has n steps.
Let 0, 1 {#}, and let . f is said to be a Turing computable function if such that, for any , ├ Turing Computable Functions M is, then, said to compute f.
Head w # # Head Head u # # Turing Computable Functions
Head Head Example # 1 1 1 1 1 1 1 1 1 # Y # #
Head Head Example # 1 1 1 1 1 1 1 1 1 # 1 N # #
h R / q5 Y / N / q6 q4 R / # R / # R / # > L / # / 1 L / # / 1 L / # / 1 q0 q1 q11 q2 q21 q3 q31 0 / 0 0 / 0 0 / 0 q7 0 / 0 L / Example
h R / q5 Y / N / q6 q4 R / # R / # R / # > L / # / 1 L / # / 1 L / # / 1 q0 q1 q11 q2 q21 q3 q31 0 / 0 0 / 0 0 / 0 q7 0 / 0 L / • Write the state transition table. Exercise
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 大同大學資工所 智慧型多媒體研究室
RYR RNR # # # >L #L # #L 1 1 1 0 0 0 0 0 Simplified Notation
> > > a/ L/ R/ q0 q0 q0 h h h Basic Turing-Machines • ||symbol-writing machines: • Head-moving machines
> > > ?/? ?/? ?/? q10 q20 q10 q1m q2n q1m h h q1h > ?/? q30 q3p h a 2. b a/a ?/? q20 q2n h > ?/? ?/? q10 q1m q20 q2n h b/b ?/? q30 q3p Combining Rules >M1 >M3 >M2 1. >M1 M2 >M1M2
Lecture 7: Turning Machines Some Example of Powerful TMs 大同大學資工所 智慧型多媒體研究室
1. 3. >R >L 2. 4. >R >L Some Powerful TMs
a b >R R >R R a, b, c, # c # Abbreviation >RR
├ Head # a b c # Head # # a b c # a b c Example: (Copier)
├ >L# R #R# R#sL# L#s # R# Example: (Copier) # a b c # # a b c # # a b c # # # b c # # # b c # # # # b c # a # # b c # a # a b c # a # a b c # a
├ Head # a b c # Head a b c # Example: (Shift-Left)
├ >L# R LsR # L# Example: (Shift-Left) # a b c # # a b c # # a b c # # a b c # a a b c # a a b c # a a b c # a b b c # …
Head ├ # a b b # a Head # Y # Example: (Palindrome)
Head ├ # a b a # a Head # N # Example: (Palindrome)
├ >SRL# LaRR #R# L #L# # # #L L# #RNR L# #RYR Example: (Palindrome) # a b b a # # # a b b a # (SR) # # a b b a # a # a b b a # a # a b b a # a # a b b a # a # # b b a # a # # b b a # a # # b b a # a # # b b # # a # # b b # # …
4. Exercises
5. 6. Exercises where w and v are strings of decimal numbers such that