103
Lecture 7: Turning Machines 虞虞虞 虞虞虞虞虞虞虞 虞虞虞虞虞 虞虞虞

Lecture 7: Turning Machines

  • Upload
    pabla

  • View
    45

  • Download
    0

Embed Size (px)

DESCRIPTION

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. - PowerPoint PPT Presentation

Citation preview

Page 1: Lecture 7: Turning Machines

Lecture 7: Turning Machines

虞台文大同大學資工所智慧型多媒體研究室

Page 2: 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

Page 3: Lecture 7: Turning Machines

Lecture 7: Turning Machines

An Overview on Finite State Machine

大同大學資工所智慧型多媒體研究室

Page 4: Lecture 7: Turning Machines

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.

Page 5: Lecture 7: Turning Machines

q0 q1

q2 q3

0

0

0

0

1 1 1 1

Examples:

00100011<CR>

01101011<CR>

001010101<CR>

0010010101<CR>

Finite State Machine

Page 6: Lecture 7: Turning Machines

Finite State Machine

q0 q1

q2 q3

0

0

0

0

1 1 1 1

stat

e

q0

q1

q2

q3

symbol(event)

0 1 <CR>q1

q0

q3

q2

q2

q3

q0

q1

ok

err

err

err

The ParserThe Parser

Page 7: Lecture 7: Turning Machines

Implementation (I)

stat

e

q0

q1

q2

q3

symbol(event)

0 1 <CR>q1

q0

q3

q2

q2

q3

q0

q1

ok

err

err

err

The ParserThe Parser

#define q0 0#define q1 1#define q2 2#define q3 3#define fini4

#define q0 0#define q1 1#define q2 2#define q3 3#define fini4

Page 8: Lecture 7: Turning Machines

Implementation (I)

stat

e

q0

q1

q2

q3

symbol(event)

0 1 <CR>q1

q0

q3

q2

q2

q3

q0

q1

ok

err

err

err

The ParserThe Parser

int parser[4][3]={q1, q2, fini,q0, q3, fini,q3, q0, fini,q2, q1, fini

};

int state=q0;int event;char* str;

int parser[4][3]={q1, q2, fini,q0, q3, fini,q3, q0, fini,q2, q1, fini

};

int state=q0;int event;char* str;

Page 9: Lecture 7: Turning Machines

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) EncodingEvent (or Message) Encoding

Page 10: Lecture 7: Turning Machines

Implementation (I)

void main(){

// Ask user to input a 0/1 string// Store the string into strstate = q0; //initializationwhile(state!=fini){

ToEvent(*str++);EventHandler(event);

}}

void main(){

// Ask user to input a 0/1 string// Store the string into strstate = q0; //initializationwhile(state!=fini){

ToEvent(*str++);EventHandler(event);

}} Event (or Message) LoopEvent (or Message) Loop

Page 11: Lecture 7: Turning Machines

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 HandlerEvent Handler

Page 12: Lecture 7: Turning Machines

Implementation (II)

q0 q1

q2 q3

0

0

0

0

1 1 1 1

stat

e

q0

q1

q2

q3

symbol(event)

0 1 <CR>pq1

pq0

pq3

pq2

pq2

pq3

pq0

pq1

ok

err

err

err

The ParserThe Parser

Page 13: Lecture 7: Turning Machines

Implementation (II)

stat

e

q0

q1

q2

q3

symbol(event)

0 1 <CR>pq1

pq0

pq3

pq2

pq2

pq3

pq0

pq1

ok

err

err

err

The ParserThe Parser

#define q0 0#define q1 1#define q2 2#define q3 3#define fini4

#define q0 0#define q1 1#define q2 2#define q3 3#define fini4

void pq0(), pq1(), pq2(), pq3();void ok(), err();void pq0(), pq1(), pq2(), pq3();void ok(), err();

Page 14: Lecture 7: Turning Machines

Implementation (II)

stat

e

q0

q1

q2

q3

symbol(event)

0 1 <CR>pq1

pq0

pq3

pq2

pq2

pq3

pq0

pq1

ok

err

err

err

The ParserThe 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;}

Page 15: Lecture 7: Turning Machines

Implementation (II)

stat

e

q0

q1

q2

q3

symbol(event)

0 1 <CR>pq1

pq0

pq3

pq2

pq2

pq3

pq0

pq1

ok

err

err

err

The ParserThe 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;

}

Page 16: Lecture 7: Turning Machines

Implementation (II)

stat

e

q0

q1

q2

q3

symbol(event)

0 1 <CR>pq1

pq0

pq3

pq2

pq2

pq3

pq0

pq1

ok

err

err

err

The ParserThe 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

};

Page 17: Lecture 7: Turning Machines

Implementation (II)

void main(){

// Ask user to input a 0/1 string// Store the string into strstate = q0; //initializationwhile(state!=fini){

ToEvent(*str++);(*parser[state][event])();

}}

void main(){

// Ask user to input a 0/1 string// Store the string into strstate = q0; //initializationwhile(state!=fini){

ToEvent(*str++);(*parser[state][event])();

}} Event (or Message) LoopEvent (or Message) Loop

Page 18: Lecture 7: Turning Machines

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.

Page 19: Lecture 7: Turning Machines

Lecture 7: Turning Machines

The Definition of Turing Machine

大同大學資工所智慧型多媒體研究室

Page 20: Lecture 7: Turning Machines

Definition

# #

Head

A Turing machine is a quadruple( , , , )K sK: finite set of states, hK. : alphabet, #, L, R.

s: sK, initial state.: transition function

Ne

ScannedSymbol

CurrentState Action

Takenw

State

: , }{ {}K Rh LK

Hang

Page 21: Lecture 7: Turning Machines

The Transition Function

( , ) ( , )q a p b

,q K a

1. Change state from q to p.

2. b print b;b{L, R} Move head in the direction of b.

Ne

ScannedSymbol

CurrentState Action

Takenw

State

: , }{ {}K Rh LK

Page 22: Lecture 7: Turning Machines

The Transition Function

( , ) ( , )q a p b

,q K a K

. . . . . . . . . . . . . .a

. . . . .

. . . . .

q ( , )p b

Ne

ScannedSymbol

CurrentState Action

Takenw

State

: , }{ {}K Rh LK

Page 23: Lecture 7: Turning Machines

Memory Configuration

w a #

Head

u #

( , , , )q w a u or ( , )q wau

The configuration of a Turing machineis a member of

( , , , )M K s

* #*w

q ua

K h

Page 24: Lecture 7: Turning Machines

Halt Configuration

w a #

Head

u #

( , , , )wh a u or ( , )h wau

The configuration of a Turing machineis a member of

( , , , )M K s

* #*w

q ua

K h

Page 25: Lecture 7: Turning Machines

Lecture 7: Turning Machines

Computing with Turing Machine

大同大學資工所智慧型多媒體研究室

Page 26: Lecture 7: Turning Machines

Yields in One Step├M

( , , , ).M K s Let

Then, 1 1 1 1 2 2 2 2( , , , ) ( , , , )q w a u q w a u├M

if and only if 1 1 2( , ) ( , )q a q b , where { , }b L R

such that

bw1 a1 u1

w2=w1 a2 u2=u1

b Lw1=w2a2 a1 u1

w2 a2 u2=a1u1

b Rw1 a1 u1=a2u2

w2=w1a1 a2 u2

Used to trace the computation sequence.

Page 27: Lecture 7: Turning Machines

Yields*M├

where Ci denotes the configuration of M. We say that co

mputation sequence (1) is of length n or has n steps.

0 1 nC C C

( , , , ).M K s Let is the reflexive, transitive

closure of ├M , i.e., if, for some n 0,

*M├

├M

0 nC C*M├

(1)├M ├M

Page 28: Lecture 7: Turning Machines

Turing Computable Functions

Let 0, 1 {#}, and let .

f is said to be a Turing computable function

if

such that, for any ,

* *0 1:f

( , , , )M K s *0w

( )f w u ( , # #) ( , # #)s w h u*M

M is, then, said to compute f.

Page 29: Lecture 7: Turning Machines

Turing Computable Functions

w

Head

# #

u

Head

# #

Head

( )f w u

( , # #)s w

( , # #)h u

Page 30: Lecture 7: Turning Machines

Example

31

( ) 1 , 3

n

m

Y w

f w N w m n

otherwise

Head

# 1 1 1 1 1 1 1 1 1 #

Head

# Y #

Page 31: Lecture 7: Turning Machines

Example

31

( ) 1 , 3

n

m

Y w

f w N w m n

otherwise

Head

# 1 1 1 1 1 1 1 1 1 #

Head

# N #

1

Page 32: Lecture 7: Turning Machines

Example

31

( ) 1 , 3

n

m

Y w

f w N w m n

otherwise

q0 q1 q11 q2 q21 q3 q31

q7

q6q4

q5

h

> L / # / 1 L / # / 1 L / # / 1

R / #

Y /

R / #

R / #

N /

R /

0 / 00 / 0

0 / 0

0 / 0

L /

HeadHead

# 1 1 1 1 1 1 1 1 1 #1

Page 33: Lecture 7: Turning Machines

Exercise

q0 q1 q11 q2 q21 q3 q31

q7

q6q4

q5

h

> L / # / 1 L / # / 1 L / # / 1

R / #

Y /

R / #

R / #

N /

R /

0 / 00 / 0

0 / 0

0 / 0

L /

2. Write the state transition table.

Page 34: Lecture 7: Turning Machines

Discussion

Three main usages of machines:– Compute– Decision– Accept

See textbooks for the definitions of– Turing decidability– Turing acceptability

Page 35: Lecture 7: Turning Machines

Lecture 7: Turning Machines

Turing-Machine Programming

大同大學資工所智慧型多媒體研究室

Page 36: Lecture 7: Turning Machines

Simplified Notation

q0 q1 q11 q2 q21 q3 q31

q2

q6q4

q5

h

> L / # / 1 L / # / 1 L / # / 1

R / #

Y /

R / #

R / #

N /

# /

0 / 00 / 0

0 / 0

0 / 0

L /

q0 q1 q11 q2 q21 q3 q31

q2

q6q4

q5

h

> L / # / 1 L / # / 1 L / # / 1

R / #

Y /

R / #

R / #

N /

# /

0 / 00 / 0

0 / 0

0 / 0

L /

>L #L1 #L1 #1

RYR

#

RNR

# #

0

00 0

0

Page 37: Lecture 7: Turning Machines

Basic Turing-Machines

1. || symbol-writing machines:

2. Head-moving machines

Print a q0 h> a/

Move Left

RMove ight

q0 h> L/

q0 h> R/

>a

>L

>R

Page 38: Lecture 7: Turning Machines

Combining Rules

>M1

>M2

>M3

q10 h?/?q1m>q30 h> ?/?q3p

1. >M1 M2 >M1M2

q20 h?/?q2nq10

?/?q1m>

q20 h?/?q2n>

2. 1>M 2M

3M

a

b q10 q1h?/?q1m> q20 h?/?q2n

a/a

q30

?/?

q3p

b/b

Page 39: Lecture 7: Turning Machines

Lecture 7: Turning Machines

Some Example of Powerful TMs

大同大學資工所智慧型多媒體研究室

Page 40: Lecture 7: Turning Machines

Some Powerful TMs

>R #

>R #

>L #

>L #

#>R1.

#>R2.

#>L3.

#>L4.

Page 41: Lecture 7: Turning Machines

Abbreviation

={a,b,c,#}

>R R

ab

c

#

>R Ra, b, c, # >RR

Page 42: Lecture 7: Turning Machines

Example: (Copier)

: # # # # #Copy w w w*M├

Head

# a b c #

Head

# #a b c a b c#

Page 43: Lecture 7: Turning Machines

Example: (Copier)

: # # # # #Copy w w w*M├

# a b c #

# a b c #

# a b c #

# # b c #

# # b c # a

# # b c # a

# a b c # a

# a b c # a

# # b c # # >L# R #R# R#sL# L#s

R#

s #

#

Page 44: Lecture 7: Turning Machines

Example: (Shift-Left)

- : # # #Left Shift w w*M├

Head

# a b c #

Head

a b c #

Page 45: Lecture 7: Turning Machines

Example: (Shift-Left)

- : # # #Left Shift w w*M├

# a b c #

# a b c #

# a b c #

# a b c #

a a b c #

a a b c #

a b b c #

a a b c #

>L# R LsR

L#

s #

#

Page 46: Lecture 7: Turning Machines

Example: (Palindrome)

# #: # #

# #

Y w LPalindrome w

N w L

*M├

*0 : RL w w w

Head

# a b b #a

Head

Y ##

Page 47: Lecture 7: Turning Machines

Example: (Palindrome)

# #: # #

# #

Y w LPalindrome w

N w L

*M├

*0 : RL w w w

Head

# a b a #a

Head

N ##

Page 48: Lecture 7: Turning Machines

Example: (Palindrome)

# #: # #

# #

R

R

Y w wPalindrome w

N w w

*M├

# a b b a ## # a b b 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 # #

a # a b b a #

(SR)

>SRL# LaRR #R# L #L#

L# #RYR

L# #RNR

#L

# # , #t s

#s s

#

Page 49: Lecture 7: Turning Machines

Exercises

- : # # ## #Shift Right w w3.

: # # # # #RMirror w w w4.

Page 50: Lecture 7: Turning Machines

Exercises

Y: # #

N

n n n

n n n

w a b cTrinity w

w a b c

5.

: # # # #Increase w v6.where w and v are strings of decimal numbers such that

( ) ( ) 1val v val w

Page 51: Lecture 7: Turning Machines

Lecture 7: Turning Machines

Extensions of the TM

大同大學資工所智慧型多媒體研究室

Page 52: Lecture 7: Turning Machines

Extensions of the TM

Standard TM

2-way TM

k-tape (1-way) TM

Nondeterministic TM

1-way TM

Deterministic TM’s

( , ) ( , )q a p b

Page 53: Lecture 7: Turning Machines

Two-Way Infinite Tape

w a u

Head ##

or( , , , ) ( , )q w a u q wau

Memory Configuration

Page 54: Lecture 7: Turning Machines

Lemma

1 1 1 1 1, , ,M K s a two-way TM

one-way TM that simulates

M1 as follows:

2 2 2 2 2, , ,M K s

1( , #) ( , )s w h uav1.1

*M├ 2( , # #) ( , # )s w h uav*

M├

2. M1 does not halt M2 does not halt.

Page 55: Lecture 7: Turning Machines

#

Proof

simulates

1 1 1 1 1, , ,M K s

2 2 2 2 2, , ,M K s

3 2 1 0 1 2 3

$0

11

22

33

4

##M1

M2

Page 56: Lecture 7: Turning Machines

Proof

simulates

1 1 1 1 1, , ,M K s

2 2 2 2 2, , ,M K s

###

3 2 1 0 1 2 3 3 2 1 0 1 2 3

$0

1

1

2

2

3

3

4 0

1

1

2

2

3

3

4

##M1

M2

2 ?K

2 ? 2 ?

2 ?s

Page 57: Lecture 7: Turning Machines

Proof

simulates

1 1 1 1 1, , ,M K s

2 2 2 2 2, , ,M K s

###

3 2 1 0 1 2 3 3 2 1 0 1 2 3

$0

1

1

2

2

3

3

4 0

1

1

2

2

3

3

4

##M1

M2

2 1 { } 1,2K K h

E.g., 1 0 1 2, ,K q q q

0 0

1 12

2 2

,1 , 2

,1 ,2

,1 ,2

,1 ,2

q q

q qK

q q

h h

Page 58: Lecture 7: Turning Machines

Proof

simulates

1 1 1 1 1, , ,M K s

2 2 2 2 2, , ,M K s

###

3 2 1 0 1 2 3 3 2 1 0 1 2 3

$0

1

1

2

2

3

3

4 0

1

1

2

2

3

3

4

##M1

M2

2 1 1 1 1 1 1 1$

Mark left end

Final

Operations

Head onlower track

Head onupper track

Mark halt configuration

Page 59: Lecture 7: Turning Machines

Proof

simulates

1 1 1 1 1, , ,M K s

2 2 2 2 2, , ,M K s

###

3 2 1 0 1 2 3 3 2 1 0 1 2 3

$0

1

1

2

2

3

3

4 0

1

1

2

2

3

3

4

##M1

M2

2 1 1 1 1 1 1 1$

1 0,1,# 2

(0,0), (0,1), (0,#),

(1,0), (1,1)

(0, 0), (0, 1), (0, #),

(1, 0), (1

(0,0), (0,1), (0, #),

(1,0), (1,1), (1,#),

(#,

, (1, #),

(#,0), (#,1), (#,

, 1), (1, #),

(#, 00), (#,1), ), (#, 1), (

$,0,1, #

#, #)(

,

#, #) ,# , )

E.g.,

Page 60: Lecture 7: Turning Machines

Proof ###

3 2 1 0 1 2 3 3 2 1 0 1 2 3

$0

1

1

2

2

3

3

4 0

1

1

2

2

3

3

4

##M1

M2

1. M2 simulates the input for M1 into M2 in the following way:

a b c b b c bM1#

Head

$a#

M2b#

c#

b#

b#

c#

b#

#

#

Headg

1 1: ( , #) ,1 , $ (#,#)#

wg s w s

s 2

Page 61: Lecture 7: Turning Machines

Proof ###

3 2 1 0 1 2 3 3 2 1 0 1 2 3

$0

1

1

2

2

3

3

4 0

1

1

2

2

3

3

4

##M1

M2

1. M2 simulates the input for M1 into M2 in the following way:

2. Simulate the operations of M1 on M2.3. When M2 would halt, restore the tape to “single track”

format by M1.

a b c b b c bM1#

Head

$a#

M2b#

c#

b#

b#

c#

b#

#

#

Headg

Page 62: Lecture 7: Turning Machines

Proof

1 0 1 1 0 0 1M1#

Head

0 1 2 3 412

$1

0M2

1

1

0

#

0

#

1

#

Head

#

1. Simulate M1 on upper track:

1 1, ,q a p b

Simulate the operations of M1 on M2.

2Determine

2 1

2 1 2

,1 , ,

,1 , , ,1 ,

,1 ,

p b a b

q a a p L b L

p R b R

Page 63: Lecture 7: Turning Machines

Proof

$1

0M2

1

1

0

1

0

#

1

#

Head

#

2. Simulate M1 on lower track:

1 2, ,q a p b

Simulate the operations of M1 on M2.

2Determine

1 1

2 1 2

, 2 , ,

, 2 , , , 2 ,

, 2 ,

p a b b

q a a p R b L

p L b R

1 0 1 1 0 0 1M1#

Head

0 1 2 3 41213

Page 64: Lecture 7: Turning Machines

Proof

$1

0M2

1

1

0

1

0

#

1

#

Head

#

3. Change track:

Simulate the operations of M1 on M2.

2Determine

2 ,1 ,$ ,2 ,q q R

$1

0M2

1

1

0

1

0

#

1

#

Head

#

2 , 2 ,$ ,1 ,q q R

Page 65: Lecture 7: Turning Machines

Proof

4. Extend the tape to the right:

Simulate the operations of M1 on M2.

2Determine

2 ,1 , # ,1 , (#,#)q q

$1

0M2

1

1

0

1

0

#

1

#

Head

#

$1

0M2

1

1

0

1

0

#

1

#

Head

#

2 , 2 , # ,2 , (#,#)q q

Page 66: Lecture 7: Turning Machines

Proof

4. Extend the tape to the right:

Simulate the operations of M1 on M2.

2Determine

2 ,1 , # ,1 , (#,#)q q

$1

0M2

1

1

0

1

0

#

1

#

Head

#

$1

0M2

1

1

0

1

0

#

1

#

Head

#

2 , 2 , # ,2 , (#,#)q q

#

#

#

#

Page 67: Lecture 7: Turning Machines

Proof

5. Halt and record head position:

Simulate the operations of M1 on M2.

2Determine

2 1 2 1 2,1 , ( , ) ,1 , ( , )h a a h a a

$1

0M2

1

1

0

1

0

#

1

#

Head

#

$1

0M2

1

1

0

1

0

#

1

#

Head

#

2 1 2 1 2, 2 , ( , ) , 2 , ( , )h a a h a a

Page 68: Lecture 7: Turning Machines

Proof

5. Halt and record head position:

Simulate the operations of M1 on M2.

2Determine

2 1 2 1 2,1 , ( , ) ,1 , ( , )h a a h a a

$1

0M2

1

1

0

1

0

#

1

#

Head

#

$1

0M2

1

1

0

1

0

#

1

#

Head

#

2 1 2 1 2, 2 , ( , ) , 2 , ( , )h a a h a a

Page 69: Lecture 7: Turning Machines

Proof1. M2 simulates the input for M1 into M2 in the following

way:

2. Simulate the operations of M1 on M2.3. When M2 would halt, restore the tape to “single track”

format by M1.

a b c b b c bM1#

Head

$a#

M2b#

c#

b#

b#

c#

b#

#

#

Headg

Page 70: Lecture 7: Turning Machines

Proof1. M2 simulates the input for M1 into M2 in the following

way:

2. Simulate the operations of M1 on M2.3. When M2 would halt, restore the tape to “single track”

format by M1.

a b c b b c bM1#

Head

$a#

M2b#

c#

b#

b#

c#

b#

#

#

Headg

$1

0M2

1

1

0

1

0

#

1

#

Head

#

#M2 #1 1 0 1 1 0 0 1

Head

Page 71: Lecture 7: Turning Machines

Lemma

1 1 1 1 1, , ,M K s a two-way TM

one-way TM that simulates

M1 as follows:

2 2 2 2 2, , ,M K s

1( , #) ( , )s w h uav1.*M├ 2( , # #) ( , # )s w h uav*

M├

2. M1 does not halt M2 does not halt.

Page 72: Lecture 7: Turning Machines

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.

Page 73: Lecture 7: Turning Machines

k-Tape TM

Usually for I/O

Usually for working memory

Head

Head

Head

. . .

MemoryConfiguration 1 1 1 2 2 2, , , , k k kq w a u w a u w a u

Page 74: Lecture 7: Turning Machines

Example (Duplicate)

: # # # # #Duplicate w w w

w# #

#

w# #

#

w# #

# w #

w# #

# w #

w# #

# w #

w #

(1)#L (2)# (1) (2)R R (2)s

(2)#L (2)R (1) (1)s R

(1) #s

(2) #s

(1)#

Page 75: Lecture 7: Turning Machines

LemmaM1: k-tape TM, k > 0

: alphabet of M1

s1: initial state

Standard TM 2 2 2 2, , ,K s

such that

1. M1 halts on input w, i.e., 1, # #, #, , #s w 1

*M├ 1 1 1, , , k k kh w a u w a u

Then, for M2, 2 , # #s w2

*M├ 1 1 1,h w a u

2. M1 hangs on w M2 too.

3. M1 neither halts nor hangs on w M2 too.

Page 76: Lecture 7: Turning Machines

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.

Page 77: Lecture 7: Turning Machines

Lecture 7: Turning Machines

Nondeterministic Turing Machine

大同大學資工所智慧型多媒體研究室

Page 78: Lecture 7: Turning Machines

NTM Definition

A Turing machine is a quadruple( , , , )K s K: finite set of states, hK.

: alphabet, #, L, R.

s: sK, initial state.

: transition function

{ { , }}K h L RK

The same as standard TM.

Page 79: Lecture 7: Turning Machines

Transition Function

{ { , }}K h L RK

E.g.,

1 1 2 2( , ) ( , ), ( , ), , ( , )r rq a p b p b p b

K

. . . . . . . . . . . . . .a

. . . . .

. . . . .

q1 1

2 2

( , )

( , )

( , )r r

p b

p b

p b

p1

p2

pr

aa

a

q

Page 80: Lecture 7: Turning Machines

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.

Page 81: Lecture 7: Turning Machines

Example

*{ , } contains substring L w a a bb w bba a regular language

Head

# a b #b a b

Head

# a b #a a a

There is a terminating path.

There is no a terminating path.

Page 82: Lecture 7: Turning Machines

Example

*{ , } contains substring L w a a bb w bba a regular language

Head

# a b #b a b

HeadHead

# a b #b a b

L

a, b

L L L L ab a b b a

#

# b, # a, # a, # b, #

Page 83: Lecture 7: Turning Machines

Example

is a composite numbernL I n

n is a composite number iff

2 such that

2

pn pq

q

Page 84: Lecture 7: Turning Machines

Example

is a composite numbernL I n

L can be accepted by the NTM

>GGPEGenerate p 2

Generate q 2

Compute m = pq

Compare m and n

Page 85: Lecture 7: Turning Machines

Example

is a composite numbernL I n

L can be accepted by the NTM

>GGPE# I I I I I I I I I I #

Head

n = 10

Page 86: Lecture 7: Turning Machines

Example

is a composite numbernL I n

L can be accepted by the NTM

>GGPE# I I I I I I I I I I #

Head

n = 10

G: generate a random number larger than 2.

>RIR IR ##

#

Page 87: Lecture 7: Turning Machines

Example

is a composite numbernL I n

L can be accepted by the NTM

>GGPE# I I I I I I I I I I #

Head

n = 10

G: generate a random number larger than 2.

I I #

p = 2

>RIR IR ##

#

Page 88: Lecture 7: Turning Machines

Example

is a composite numbernL I n

L can be accepted by the NTM

>GGPE# I I I I I I I I I I #

Head

n = 10

G: generate a random number larger than 2.

I I # I I I I I #

p = 2 q = 5

>RIR IR ##

#

Page 89: Lecture 7: Turning Machines

Example

is a composite numbernL I n

L can be accepted by the NTM

>GGPE# I I I I I I I I I I #

Head

n = 10

P: product

I I # I I I I I #

p = 2 q = 5

: # # # # #p q pqP I I I

Page 90: Lecture 7: Turning Machines

Example

is a composite numbernL I n

L can be accepted by the NTM

>GGPE# I I I I I I I I I I #

Head

n = 10

P: product

I I #

m = pq = 10

: # # # # #p q pqP I I I

I I I I I I I I

Page 91: Lecture 7: Turning Machines

Example

is a composite numbernL I n

L can be accepted by the NTM

>GGPE# I I I I I I I I I I #

Head

n = 10

P: product

I I #

m = pq = 10

: # # # # #p q pqP I I I

I I I I I I I I

Page 92: Lecture 7: Turning Machines

Example

is a composite numbernL I n

L can be accepted by the NTM

>GGPE# I I I I I I I I I I #

Head

n = 10

E: equivalence

I I #

m = pq = 10

: # # #m n halt m nE I I

m n

I I I I I I I I

Page 93: Lecture 7: Turning Machines

Example

is a composite numbernL I n

L can be accepted by the NTM

>GGPE# I I I I I I I I I I #

Head

n = 10

I I #

m = pq = 10

I I I I I I I I

n is a composite number iff2

such that 2

pn pq

q

Page 94: Lecture 7: Turning Machines

Lemma

For every NTM , we can

construct a standard TM such that for any

1 1 1 1( , , , )M K s

2M *

1 #w

1. if M1 halts on w, then M2 is also so;

2. if M1 does not halts on w, then M2 is also not.

Page 95: Lecture 7: Turning Machines

Proof1 1 1 1( , , , )M K s

2 ?M

K1

1

. . . . . . . . . . . . . .a

. . . . .

. . . . .

q1 1

2 2( , )

( , )

( , )

( , )

q a

k k

p b

p bA

p b

1 { , }L R

1 { }K h

( , ) 1 11 2q aA K r ( , ) 1 11 2q aA K r

Numbering:

p1

p2

p3

a

a

a

q

( , ) 3, 10q aA r

1

2

3, 4, …,10

Page 96: Lecture 7: Turning Machines

Proof1 1 1 1( , , , )M K s

2 ?M

Computation path indexing

>

d1

d2

d3

d1

d2

d1

d2

d3

d4

Page 97: Lecture 7: Turning Machines

Proof1 1 1 1( , , , )M K s

2 ?M

An computation path (#d2d1d4#)

>

d1

d2

d3

d1

d2

d1

d2

d3

d4

Page 98: Lecture 7: Turning Machines

Proof1 1 1 1( , , , )M K s

2 ?M

G : computation path generation

## #d1#

#d2#

#dr#

#d1d1#

#d1d2#

#d1dr#

…#drdr#

…#d2d1#

#d1d1d1#

#d1d1d2#

#d1drdr#…

#drdrdr#

…#d2d1d1#

#d1d1d1d1#

#d1d1d1d2#

#d1drdrdr#

#drdrdrdr#

…#d2d1d1d1#

……

……

Page 99: Lecture 7: Turning Machines

Proof1 1 1 1( , , , )M K s

2 ?M

# w

Head

#1M

1st tape: never changed.

2nd tape: simulate the computation of M1.

3rd tape: computation path.

# w

Head

#

$ w

Head

#

#

Head

2M #

Page 100: Lecture 7: Turning Machines

Proof# w

Head

#1M

# w

Head

#

$ w

Head

#

#

HeadHead

2M 2M #

Copy tape1 to tape 2.

>$(2)R(2)R(3)C M1’ G#(3)

Page 101: Lecture 7: Turning Machines

Proof

>$(2)R(2)R(3)C M1’ G#(3)

# w

Head

#1M

# w

Head

#

$ w

Head

#

#

HeadHead

2M 2M #

Copy tape1 to tape 2.

Simulate M1 on tape

2 according to the computation path

depicted on tape 3.

Generate computation

path.

# w’

Head

#1M

# w

Head

#

$ w’

Head

#

#

HeadHead

2M 2M #

d1 d5 d2 d3 d1 d2 #

halt

Page 102: Lecture 7: Turning Machines

# w’

Head

#1M

# w

Head

#

$ w’

Head

#

#

HeadHead

2M 2M #

d1 d5 d2 d3 d1 d2 #

Page 103: Lecture 7: Turning Machines

Lemma

For every NTM , we can

construct a standard TM such that for any

1 1 1 1( , , , )M K s

2M *

1 #w

1. if M1 halts on w, then M2 is also so;

2. if M1 does not halts on w, then M2 is also not.