Presentation is loading. Please wait.

Presentation is loading. Please wait.

Lecture 5: Turning Machine

Similar presentations


Presentation on theme: "Lecture 5: Turning Machine"— Presentation transcript:

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

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 5: Turning Machine
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. (legal) (illegal) Writing a C program to do so.

5 Finite State Machine Examples: q0 q1 q2 q3 1 00100011<CR>
1 Examples: <CR> <CR> <CR> <CR>

6 Finite State Machine state q0 q1 q2 q3 symbol (event) 1 <CR> q0
1 <CR> q0 q1 q2 q3 1 q1 q2 ok q0 q3 err q3 q0 err q2 q1 err The Parser

7 Implementation (I) state q0 q1 q2 q3 symbol (event) 1 <CR> ok
1 <CR> ok err The Parser #define q0 0 #define q1 1 #define q2 2 #define q3 3 #define fini 4

8 Implementation (I) int parser[4][3]={ q1, q2, fini, q0, q3, fini,
}; int state=q0; int event; char* str; state q0 q1 q2 q3 symbol (event) 1 <CR> ok err The Parser

9 Implementation (I) 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) Event (or Message) Loop 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) Event Handler 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) state q0 q1 q2 q3 symbol (event) 1 <CR> q0
1 <CR> q0 q1 q2 q3 1 pq1 pq2 ok pq0 pq3 err pq3 pq0 err pq2 pq1 err The Parser

13 Implementation (II) state q0 q1 q2 q3 symbol (event) 1 <CR> pq1
1 <CR> pq1 pq0 pq3 pq2 ok err The Parser #define q0 0 #define q1 1 #define q2 2 #define q3 3 #define fini 4 void pq0(), pq1(), pq2(), pq3(); void ok(), err();

14 Implementation (II) state q0 q1 q2 q3 symbol (event) 1 <CR> pq1
1 <CR> pq1 pq0 pq3 pq2 ok err The Parser void pq0() { state = q0; } void pq1() state = q1; void pq2() { state = q2; } void pq3() state = q3;

15 Implementation (II) void ok() { printf(”ok\n”); state = fini; }
void err() printf(”error\n”); state q0 q1 q2 q3 symbol (event) 1 <CR> pq1 pq0 pq3 pq2 ok err The Parser

16 Implementation (II) typedef void (*FUNCTION)();
FUNCTION parser[4][3]={ pq1, pq2, ok, pq0, pq3, err, pq3, pq0, err, pq2, pq1, err }; Implementation (II) state q0 q1 q2 q3 symbol (event) 1 <CR> pq1 pq0 pq3 pq2 ok err The Parser

17 Implementation (II) Event (or Message) Loop 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 Lecture 5: Turning Machine
The Definition of Turing Machine 大同大學資工所 智慧型多媒體研究室

19 Definition A Turing machine is a quadruple
# Head Hang A Turing machine is a quadruple K: finite set of states, hK. : alphabet, #, L, R. : transition function s: sK, initial state.

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

21 The Transition Function
K a q

22 Memory Configuration w a # u  # or
Head u  # or The configuration of a Turing machine is a member of

23 Halt Configuration w a # u  # or
Head u  # or The configuration of a Turing machine is a member of

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

25 Yields in One Step├M Let Then, if and only if , where such that
Used to trace the computation sequence. Yields in One Step├M Let ├M Then, if and only if , where such that w1 a1 u1 w1=w2a2 a1 u1 w1 a1 u1=a2u2 w2=w1 a2 u2=u1 w2 a2 u2=a1u1 w2=w1a1 a2 u2

26 Yields Let is the reflexive, transitive closure of ├M , i.e., if, for some n  0, ├M (1) where Ci denotes the configuration of M. We say that computation sequence (1) is of length n or has n steps.

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

28 Turing Computable Functions
w Head # u Head #

29 Example Head # 1 1 1 1 1 1 1 1 1 # Head # Y #

30 Example Head # 1 1 1 1 1 1 1 1 1 1 # Head # N #

31 Example > q0 q1 q11 q2 q21 q3 q31 q7 q6 q4 q5 h L / # / 1 R / # Y /
N / R / 0 / 0

32 Exercise > Write the state transition table. q0 q1 q11 q2 q21 q3
# / 1 R / # Y / N / R / 0 / 0

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

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

35 Simplified Notation >L #L 1 # RYR RNR

36 Basic Turing-Machine > > > || symbol-writing machines:
Head-moving machines q0 h > a/ q0 h > L/ q0 h > R/

37 Combining Rules > > > > > >M1 >M3 >M2
q10 h ?/? q1m > >M3 q30 h > ?/? q3p >M2 q20 h ?/? q2n > 1. >M1 M2  >M1M2 q20 h ?/? q2n q10 q1m > 2. a b q10 q1h ?/? q1m > q20 h q2n a/a q30 q3p b/b

38 Lecture 5: Turning Machine
Some Example of Powerful TMs 大同大學資工所 智慧型多媒體研究室

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

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

41 Example: (Copier) Head # a b c Head # a b c

42 Example: (Copier) ├ >L# R #R# R#sL# L#s R# # a b c # # a b c #

43 Example: (Left-Shift)
Head # a b c Head a b c #

44 Example: (Left-Shift)
Example: (Left-Shift) # a b c # # a b c # # a b c # # a b c # >L# R LsR L# # a a b c # a a b c # a a b c # a b b c #

45 Example: (Palindrome)
Head # a b Head Y #

46 Example: (Palindrome)
Head # a b Head N #

47 Example: (Palindrome)
Example: (Palindrome) # a b b a # # # a b b a # (SR) # # a b b a # a # a b b a # >SRL# LaRR #R# L #L# L# #RYR L# #RNR #L # 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 # #

48 Exercises 1. 2.

49 Lecture 5: Turning Machine
Extensions of the TM 大同大學資工所 智慧型多媒體研究室

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

51 Two-Way Infinite Tape w a u Head Memory Configuration

52 Lemma 1. ├ ├ 2. M1 does not halt M2 does not halt. a two-way TM
one-way TM that simulates M1 as follows: 1. 2. M1 does not halt M2 does not halt.

53 Proof   # # # M1 M2 3 2 1 1 2 3 $ 1 1 2 2 3 3 4
simulates 3 2 1 1 2 3  M1 # # # $  1 1 2 2 3 3 4 M2

54 Proof simulates

55 Proof simulates E.g.,

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

57 Proof simulates E.g.,

58 Proof M2 simulates the input for M1 into M2 in the following way: a b c M1 # Head g $ a # M2 b c Head s2

59 Proof M2 simulates the input for M1 into M2 in the following way: Simulate the operations of M1 on M2. When M2 would halt, restore the tape to “single track” format by M1. a b c M1 # Head g $ a # M2 b c Head

60 Proof M1 M2 Simulate the operations of M1 on M2. Determine #
M1 # Head 2 3 4 1 2 $ 1 M2 # Head 1. Simulate M1 on upper track:

61 Proof M1 M2 Simulate the operations of M1 on M2. Determine #
M1 # Head 2 3 4 1 2 3 $ 1 M2 # Head 2. Simulate M1 on lower track:

62 Proof M2 M2 Simulate the operations of M1 on M2. Determine
$ 1 M2 # Head $ 1 M2 # Head 3. Change track:

63 Proof M2 Simulate the operations of M1 on M2. Determine
$ 1 M2 # Head 4. Extend the tape to the right:

64 Proof M2 Simulate the operations of M1 on M2. Determine # #
$ 1 M2 # Head # # 4. Extend the tape to the right:

65 Proof M2 Simulate the operations of M1 on M2. Determine
$ 1 M2 # Head 5. Halt and record head position:

66 Proof M2 M2 Simulate the operations of M1 on M2. Determine # #
Head $ 1 1 1 # M2 1 1 # # $ 1 1 1 # M2 1 1 # # Head 5. Halt and record head position:

67 Proof M2 simulates the input for M1 into M2 in the following way: Simulate the operations of M1 on M2. When M2 would halt, restore the tape to “single track” format by M1. a b c M1 # Head g $ a # M2 b c Head

68 Proof M2 simulates the input for M1 into M2 in the following way: Simulate the operations of M1 on M2. When M2 would halt, restore the tape to “single track” format by M1. $ 1 M2 # Head a b c M1 # Head g # M2 1 Head $ a # M2 b c Head

69 Lemma 1. ├ ├ 2. M1 does not halt M2 does not halt. a two-way TM
one-way TM that simulates M1 as follows: 1. 2. M1 does not halt M2 does not halt.

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

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

72 Example (Duplicate) w # w # w # w # w #

73 Lemma M1: k-tape TM, k > 0 : alphabet of M1  Standard TM
s1: initial state such that 1. M1 halts on input w, i.e., Then, for M2, 2. M1 hangs on w  M2 too. 3. M1 neither halts nor hangs on w  M2 too.

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

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

76 NTM Definition A Turing machine is a quadruple
K: finite set of states, hK. The same as standard TM. : alphabet, #, L, R. s: sK, initial state. : transition function

77 Transition Function  a  K q p1 p2 q pr E.g., a a a . . . . . . .

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

79 Example   a regular language There is a terminating path.
Head #  a b There is a terminating path. Head #  a b There is no a terminating path.

80 Example a regular language a, b b a # b, # a, #

81 Example n is a composite number iff

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

83 Example L can be accepted by the NTM >GGPE # I Head n = 10

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

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

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

87 >GGPE Example P: product L can be accepted by the NTM n = 10 q = 5
Head # I I I I I I I I I I # I I # I I I I I # q = 5 p = 2

88 >GGPE Example P: product L can be accepted by the NTM n = 10
m = pq = 10 Head # I I I I I I I I I I # I I I I I I I I I I #

89 >GGPE Example P: product L can be accepted by the NTM n = 10
m = pq = 10 Head # I I I I I I I I I I # I I I I I I I I I I #

90 >GGPE Example E: equivalence L can be accepted by the NTM n = 10
m = pq = 10 Head # I I I I I I I I I I # I I I I I I I I I I #

91 >GGPE Example L can be accepted by the NTM
n is a composite number iff Example L can be accepted by the NTM >GGPE n = 10 m = pq = 10 Head # I I I I I I I I I I # I I I I I I I I I I #

92 Lemma For every NTM , we can construct a standard TM such that for any
if M1 halts on w, then M2 is also so; if M1 does not halts on w, then M2 is also not.

93 Proof 1  a K1 q p1 p2 q p3 Numbering: a 1 2 3, 4, …,10 . . . . . . .

94 Proof Computation path indexing > d1 d2 d3 d4

95 Proof An computation path (#d2d1d4#) > d1 d2 d3 d4

96 Proof … … … … … G : computation path generation ## #d1# #d2# #dr#
#drdr# #d2d1# #d1d1d1# #d1d1d2# #d1drdr# #drdrdr# #d2d1d1# #d1d1d1d1# #d1d1d1d2# #d1drdrdr# #drdrdrdr# #d2d1d1d1#

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

98 Proof >$(2)R(2)R(3)C M1’ G #(3) Copy tape1 to tape 2.

99 Generate computation path.
Proof >$(2)R(2)R(3)C M1’ G #(3) Simulate M1 on tape 2 according to the computation path depicted on tape 3. halt Copy tape1 to tape 2. Generate computation path.

100

101 Lemma For every NTM , we can construct a standard TM such that for any
if M1 halts on w, then M2 is also so; if M1 does not halts on w, then M2 is also not.


Download ppt "Lecture 5: Turning Machine"

Similar presentations


Ads by Google