4.3 programming techniques for turing machines example 4...

102
Example 4. Design a Turing machine that computes f (m, n). f (m, n)= max(m - n, 0) = if m n then m - n else 0 0 1 B q 0 (q 1 ,B,R) (q 5 ,B,R) q 1 (q 1 , 0,R) (q 2 , 1,R) q 2 (q 3 , 1,L) (q 2 , 1,R) (q 4 ,B,L) q 3 (q 3 , 0,L) (q 3 , 1,L) (q 0 ,B,R) q 4 (q 4 , 0,L) (q 4 ,B,L) (q 6 , 0,R) q 5 (q 5 ,B,R) (q 5 ,B,R) (q 6 ,B,R) q 6 M =({q 0 ,q 1 ,...,q 6 }, {0, 1}, {0, 1,B }, δ,q 0 ,B, {q 6 })

Upload: others

Post on 15-Sep-2020

2 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

Example 4.

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes f(m,n).

f(m,n) = max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B)

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes f(m,n).

f(m,n) = max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B)

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes max(m� n, 0).

max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B)

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes f(m,n).

f(m,n) = max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B, {q6})

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

Page 2: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

… B 0 0 0 1 0 B B …

q0

Initial machine configuration: e.g., f(3,1)

… B 0 0 B B B B B …

When the machine halts:

Page 3: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

q0

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes max(m� n, 0).

max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B)

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

… B 0 0 0 1 0 B B …

e.g., f(3,2)

Page 4: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

q1

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes max(m� n, 0).

max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B)

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

… B B 0 0 1 0 B B …

Page 5: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

q1

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes max(m� n, 0).

max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B)

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

… B B 0 0 1 0 B B …

Page 6: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

q1

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes max(m� n, 0).

max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B)

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

… B B 0 0 1 0 B B …

Page 7: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

q2

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes max(m� n, 0).

max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B)

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

… B B 0 0 1 0 B B …

Page 8: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

q3

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes max(m� n, 0).

max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B)

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

… B B 0 0 1 1 B B …

Page 9: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

q3

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes max(m� n, 0).

max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B)

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

… B B 0 0 1 1 B B …

Page 10: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

q3

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes max(m� n, 0).

max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B)

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

… B B 0 0 1 1 B B …

Page 11: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

q3

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes max(m� n, 0).

max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B)

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

… B B 0 0 1 1 B B …

Page 12: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

q0

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes max(m� n, 0).

max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B)

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

… B B 0 0 1 1 B B …

Page 13: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

q1

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes max(m� n, 0).

max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B)

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

… B B B 0 1 1 B B …

Page 14: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

q1

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes max(m� n, 0).

max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B)

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

… B B B 0 1 1 B B …

Page 15: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

q2

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes max(m� n, 0).

max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B)

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

… B B B 0 1 1 B B …

Page 16: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

q2

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes max(m� n, 0).

max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B)

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

… B B B 0 1 1 B B …

Page 17: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

q4

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes max(m� n, 0).

max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B)

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

… B B B 0 1 1 B B …

Page 18: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

q4

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes max(m� n, 0).

max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B)

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

… B B B 0 1 B B B …

Page 19: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

q4

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes max(m� n, 0).

max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B)

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

… B B B 0 B B B B …

Page 20: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

q4

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes max(m� n, 0).

max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B)

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

… B B B 0 B B B B …

Page 21: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

q6

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes max(m� n, 0).

max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B)

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

… B B 0 0 B B B B …

Page 22: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

q0

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes max(m� n, 0).

max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B)

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

… B 0 1 0 B B B B …

e.g., f(1,1)

Page 23: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

q1

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes max(m� n, 0).

max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B)

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

… B B 1 0 B B B B …

Page 24: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

q2

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes max(m� n, 0).

max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B)

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

… B B 1 0 B B B B …

Page 25: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

q3

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes max(m� n, 0).

max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B)

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

… B B 1 1 B B B B …

Page 26: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

q3

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes max(m� n, 0).

max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B)

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

… B B 1 1 B B B B …

Page 27: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

q0

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes max(m� n, 0).

max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B)

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

… B B 1 1 B B B B …

Page 28: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

q5

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes max(m� n, 0).

max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B)

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

… B B B 1 B B B B …

Page 29: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

q5

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes max(m� n, 0).

max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B)

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

… B B B B B B B B …

Page 30: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

q6

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes max(m� n, 0).

max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B)

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

… B B B B B B B B …

Page 31: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

q0

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes max(m� n, 0).

max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B)

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

… B 0 1 0 0 B B B …

e.g., f(1,2)

Page 32: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

q1

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes max(m� n, 0).

max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B)

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

… B B 1 0 0 B B B …

Page 33: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

q2

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes max(m� n, 0).

max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B)

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

… B B 1 0 0 B B B …

Page 34: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

q3

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes max(m� n, 0).

max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B)

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

… B B 1 1 0 B B B …

Page 35: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

q3

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes max(m� n, 0).

max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B)

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

… B B 1 1 0 B B B …

Page 36: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

q0

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes max(m� n, 0).

max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B)

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

… B B 1 1 0 B B B …

Page 37: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

q5

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes max(m� n, 0).

max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B)

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

… B B B 1 0 B B B …

Page 38: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

q5

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes max(m� n, 0).

max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B)

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

… B B B B 0 B B B …

Page 39: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

q5

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes max(m� n, 0).

max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B)

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

… B B B B B B B B …

Page 40: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

q6

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes max(m� n, 0).

max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B)

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

… B B B B B B B B …

Page 41: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

Example, revisited

Page 42: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

q0

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes max(m� n, 0).

max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B)

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

… B 0 0 0 1 0 B B …

e.g., f(3,2)

Scanned 0 in q0, the cycle must repeat:1. replace 0 by B2. move right3. enter q1

Page 43: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

q1

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes max(m� n, 0).

max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B)

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

… B B 0 0 1 0 B B …

In q1, search right, looking for leftmost 1

Page 44: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

q1

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes max(m� n, 0).

max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B)

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

… B B 0 0 1 0 B B …

In q1, search right, looking for leftmost 1

Page 45: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

q1

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes max(m� n, 0).

max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B)

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

… B B 0 0 1 0 B B …

When found, enter q2

Page 46: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

q2

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes max(m� n, 0).

max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B)

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

… B B 0 0 1 0 B B …

- In q2, move right until it finds 0- When found, change it by 1- enter q3

Page 47: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

q3

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes max(m� n, 0).

max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B)

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

… B B 0 0 1 1 B B …

In q3, move left until it find B

Page 48: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

q3

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes max(m� n, 0).

max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B)

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

… B B 0 0 1 1 B B …

Page 49: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

q3

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes max(m� n, 0).

max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B)

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

… B B 0 0 1 1 B B …

Page 50: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

q3

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes max(m� n, 0).

max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B)

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

… B B 0 0 1 1 B B …

When B found, enter q0 and begin the cycle

Page 51: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

q0

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes max(m� n, 0).

max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B)

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

… B B 0 0 1 1 B B …

Begin a new cycle.

Page 52: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

q1

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes max(m� n, 0).

max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B)

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

… B B B 0 1 1 B B …

look for leftmost 1

Page 53: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

q1

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes max(m� n, 0).

max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B)

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

… B B B 0 1 1 B B …

When found, enter q2

Page 54: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

q2

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes max(m� n, 0).

max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B)

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

… B B B 0 1 1 B B …

move right until it finds 0

Page 55: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

q2

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes max(m� n, 0).

max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B)

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

… B B B 0 1 1 B B …

- No more 0’s. - All n 0’s changed to 1- n+1 0’s changed to B- m-(n+1) 0’s on the tape- replace all 1’s by B and put one 0

Page 56: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

q4

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes max(m� n, 0).

max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B)

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

… B B B 0 1 1 B B …

move left, changing 1 by B, until it finds B

Page 57: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

q4

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes max(m� n, 0).

max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B)

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

… B B B 0 1 B B B …

Page 58: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

q4

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes max(m� n, 0).

max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B)

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

… B B B 0 B B B B …

Page 59: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

q4

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes max(m� n, 0).

max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B)

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

… B B B 0 B B B B …

- change that B by 0- enter the final state

Page 60: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

q6

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes max(m� n, 0).

max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B)

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

… B B 0 0 B B B B …

Page 61: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

q0

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes max(m� n, 0).

max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B)

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

… B 0 1 0 B B B B …

e.g., f(1,1)

- change 0 by B- enter q1

Page 62: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

q1

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes max(m� n, 0).

max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B)

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

… B B 1 0 B B B B …

- look for the leftmost 1- when found, enter q2

Page 63: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

q2

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes max(m� n, 0).

max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B)

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

… B B 1 0 B B B B …

- move right until it finds 0- when found, that 0 by 1- enter q3

Page 64: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

q3

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes max(m� n, 0).

max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B)

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

… B B 1 1 B B B B …

move left until it finds B

Page 65: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

q3

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes max(m� n, 0).

max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B)

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

… B B 1 1 B B B B …

when B found, begin the cycle

Page 66: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

q0

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes max(m� n, 0).

max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B)

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

… B B 1 1 B B B B …

- cannot find 0 in q0- n >= m- replace 1 by B - enter q5

Page 67: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

q5

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes max(m� n, 0).

max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B)

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

… B B B 1 B B B B …

In q5, change all 0’s and 1’s to B

Page 68: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

q5

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes max(m� n, 0).

max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B)

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

… B B B B B B B B …

enter q6

Page 69: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

q6

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes max(m� n, 0).

max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B)

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

… B B B B B B B B …

Page 70: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

Example 5.

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes f(m,n).

f(m,n) = max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B, {q6})

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

Principles (how it works):

– M repeats the following steps:1. M finds the leftmost remaining 0 and replaces it by a blank.2. It then searches right, looking for 1.3. After finding 1, it continues right, until it comes to a 0, and replaces it by 1.4. M then returns left, seeking the leftmost 0, which it identifies when it first meets a blank

and then moves one cell to the right. Now, M goes to the beginning of the cycle.– This repetition ends if either:

1. Searching right for a 0, M encounters a blank (instead of 0). This means that the n 0’sin the right group of 0’s have all been changed to 1’s, and the first n + 1 0’s have beenchanged to B. At this point, how many 0’s are on the tape? Since n+ 1 0’s among m arechanged to B, there are m� (n+1) = m�n� 1 0’s on the tape. So, the machine replacesn+ 1 1’s by one 0 and n B’s. Therefore, m� n 0’s are on the tape.

2. Beginning the cycle, M cannot find a 0. This means that the first group of 0’s have beenchanged to B. This again means that n � m, so m n = 0. M replaces all remaining 1’sand 0’s by B and ends with a completely blank tape.

Example 32. Design a Turing machine that accepts 01⇤ + 10⇤.

We can easily design a machine, if we extend the original model of Turing machine slightly. Weextend the Turing machine so that its control unit has an additional, finite amount of storage.

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes f(m,n).

f(m,n) = max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B, {q6})

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

Principles (how it works):

– M repeats the following steps:1. M finds the leftmost remaining 0 and replaces it by a blank.2. It then searches right, looking for 1.3. After finding 1, it continues right, until it comes to a 0, and replaces it by 1.4. M then returns left, seeking the leftmost 0, which it identifies when it first meets a blank

and then moves one cell to the right. Now, M goes to the beginning of the cycle.– This repetition ends if either:

1. Searching right for a 0, M encounters a blank (instead of 0). This means that the n 0’sin the right group of 0’s have all been changed to 1’s, and the first n + 1 0’s have beenchanged to B. At this point, how many 0’s are on the tape? Since n+ 1 0’s among m arechanged to B, there are m� (n+1) = m�n� 1 0’s on the tape. So, the machine replacesn+ 1 1’s by one 0 and n B’s. Therefore, m� n 0’s are on the tape.

2. Beginning the cycle, M cannot find a 0. This means that the first group of 0’s have beenchanged to B. This again means that n � m, so m n = 0. M replaces all remaining 1’sand 0’s by B and ends with a completely blank tape.

Example 32. Design a Turing machine that accepts 01⇤ + 10⇤.

M = ({q0, q1}⇥ {0, 1, B}, {0, 1}, {0, 1, B}, �, (q0, B), B, {(q1, B)})

To simplify the design, we use a trick here. We define the state of the Turing machine as thecartesian product of the ordinary states and tape symbols. Initial state is (q0, B) and (q1, B) isthe final state. Then, the transition function is defined very simply as follows:

1. �((q0, B), a) = ((q1, a), a, R) for a = 02. �((q1, a), a) = ((q1, a), a, R)3. �((q1, a), B) = ((q1, B), B,R)

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes f(m,n).

f(m,n) = max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B, {q6})

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

Principles (how it works):

– M repeats the following steps:1. M finds the leftmost remaining 0 and replaces it by a blank.2. It then searches right, looking for 1.3. After finding 1, it continues right, until it comes to a 0, and replaces it by 1.4. M then returns left, seeking the leftmost 0, which it identifies when it first meets a blank

and then moves one cell to the right. Now, M goes to the beginning of the cycle.– This repetition ends if either:

1. Searching right for a 0, M encounters a blank (instead of 0). This means that the n 0’sin the right group of 0’s have all been changed to 1’s, and the first n + 1 0’s have beenchanged to B. At this point, how many 0’s are on the tape? Since n+ 1 0’s among m arechanged to B, there are m� (n+1) = m�n� 1 0’s on the tape. So, the machine replacesn+ 1 1’s by one 0 and n B’s. Therefore, m� n 0’s are on the tape.

2. Beginning the cycle, M cannot find a 0. This means that the first group of 0’s have beenchanged to B. This again means that n � m, so m n = 0. M replaces all remaining 1’sand 0’s by B and ends with a completely blank tape.

Example 32. Design a Turing machine that accepts 01⇤ + 10⇤.

M = ({q0, q1}⇥ {0, 1, B}, {0, 1}, {0, 1, B}, �, (q0, B), B, {(q1, B)})

To simplify the design, we use a trick here. We define the state of the Turing machine as thecartesian product of the ordinary states and tape symbols. Initial state is (q0, B) and (q1, B) isthe final state. Then, the transition function is defined very simply as follows:

1. �((q0, B), a) = ((q1, a), a, R) for a = 02. �((q1, a), a) = ((q1, a), a, R)3. �((q1, a), B) = ((q1, B), B,R)

Page 71: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

… B 0 1 1 1 B B B …

q0

B

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes f(m,n).

f(m,n) = max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B, {q6})

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

Principles (how it works):

– M repeats the following steps:1. M finds the leftmost remaining 0 and replaces it by a blank.2. It then searches right, looking for 1.3. After finding 1, it continues right, until it comes to a 0, and replaces it by 1.4. M then returns left, seeking the leftmost 0, which it identifies when it first meets a blank

and then moves one cell to the right. Now, M goes to the beginning of the cycle.– This repetition ends if either:

1. Searching right for a 0, M encounters a blank (instead of 0). This means that the n 0’sin the right group of 0’s have all been changed to 1’s, and the first n + 1 0’s have beenchanged to B. At this point, how many 0’s are on the tape? Since n+ 1 0’s among m arechanged to B, there are m� (n+1) = m�n� 1 0’s on the tape. So, the machine replacesn+ 1 1’s by one 0 and n B’s. Therefore, m� n 0’s are on the tape.

2. Beginning the cycle, M cannot find a 0. This means that the first group of 0’s have beenchanged to B. This again means that n � m, so m n = 0. M replaces all remaining 1’sand 0’s by B and ends with a completely blank tape.

Example 32. Design a Turing machine that accepts 01⇤ + 10⇤.

M = ({q0, q1}⇥ {0, 1, B}, {0, 1}, {0, 1, B}, �, (q0, B), B, {(q1, B)})

To simplify the design, we use a trick here. We define the state of the Turing machine as thecartesian product of the ordinary states and tape symbols. Initial state is (q0, B) and (q1, B) isthe final state. Then, the transition function is defined very simply as follows:

1. �((q0, B), a) = ((q1, a), a, R) for a = 0 or a = 12. �((q1, a), a) = ((q1, a), a, R)3. �((q1, a), B) = ((q1, B), B,R)

Page 72: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

… B 0 1 1 1 B B B …

q1

0

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes f(m,n).

f(m,n) = max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B, {q6})

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

Principles (how it works):

– M repeats the following steps:1. M finds the leftmost remaining 0 and replaces it by a blank.2. It then searches right, looking for 1.3. After finding 1, it continues right, until it comes to a 0, and replaces it by 1.4. M then returns left, seeking the leftmost 0, which it identifies when it first meets a blank

and then moves one cell to the right. Now, M goes to the beginning of the cycle.– This repetition ends if either:

1. Searching right for a 0, M encounters a blank (instead of 0). This means that the n 0’sin the right group of 0’s have all been changed to 1’s, and the first n + 1 0’s have beenchanged to B. At this point, how many 0’s are on the tape? Since n+ 1 0’s among m arechanged to B, there are m� (n+1) = m�n� 1 0’s on the tape. So, the machine replacesn+ 1 1’s by one 0 and n B’s. Therefore, m� n 0’s are on the tape.

2. Beginning the cycle, M cannot find a 0. This means that the first group of 0’s have beenchanged to B. This again means that n � m, so m n = 0. M replaces all remaining 1’sand 0’s by B and ends with a completely blank tape.

Example 32. Design a Turing machine that accepts 01⇤ + 10⇤.

M = ({q0, q1}⇥ {0, 1, B}, {0, 1}, {0, 1, B}, �, (q0, B), B, {(q1, B)})

To simplify the design, we use a trick here. We define the state of the Turing machine as thecartesian product of the ordinary states and tape symbols. Initial state is (q0, B) and (q1, B) isthe final state. Then, the transition function is defined very simply as follows:

1. �((q0, B), a) = ((q1, a), a, R) for a = 0 or a = 12. �((q1, a), a) = ((q1, a), a, R)3. �((q1, a), B) = ((q1, B), B,R)

Page 73: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

… B 0 1 1 1 B B B …

q1

0

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes f(m,n).

f(m,n) = max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B, {q6})

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

Principles (how it works):

– M repeats the following steps:1. M finds the leftmost remaining 0 and replaces it by a blank.2. It then searches right, looking for 1.3. After finding 1, it continues right, until it comes to a 0, and replaces it by 1.4. M then returns left, seeking the leftmost 0, which it identifies when it first meets a blank

and then moves one cell to the right. Now, M goes to the beginning of the cycle.– This repetition ends if either:

1. Searching right for a 0, M encounters a blank (instead of 0). This means that the n 0’sin the right group of 0’s have all been changed to 1’s, and the first n + 1 0’s have beenchanged to B. At this point, how many 0’s are on the tape? Since n+ 1 0’s among m arechanged to B, there are m� (n+1) = m�n� 1 0’s on the tape. So, the machine replacesn+ 1 1’s by one 0 and n B’s. Therefore, m� n 0’s are on the tape.

2. Beginning the cycle, M cannot find a 0. This means that the first group of 0’s have beenchanged to B. This again means that n � m, so m n = 0. M replaces all remaining 1’sand 0’s by B and ends with a completely blank tape.

Example 32. Design a Turing machine that accepts 01⇤ + 10⇤.

M = ({q0, q1}⇥ {0, 1, B}, {0, 1}, {0, 1, B}, �, (q0, B), B, {(q1, B)})

To simplify the design, we use a trick here. We define the state of the Turing machine as thecartesian product of the ordinary states and tape symbols. Initial state is (q0, B) and (q1, B) isthe final state. Then, the transition function is defined very simply as follows:

1. �((q0, B), a) = ((q1, a), a, R) for a = 0 or a = 12. �((q1, a), a) = ((q1, a), a, R)3. �((q1, a), B) = ((q1, B), B,R)

Page 74: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

… B 0 1 1 1 B B B …

q1

0

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes f(m,n).

f(m,n) = max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B, {q6})

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

Principles (how it works):

– M repeats the following steps:1. M finds the leftmost remaining 0 and replaces it by a blank.2. It then searches right, looking for 1.3. After finding 1, it continues right, until it comes to a 0, and replaces it by 1.4. M then returns left, seeking the leftmost 0, which it identifies when it first meets a blank

and then moves one cell to the right. Now, M goes to the beginning of the cycle.– This repetition ends if either:

1. Searching right for a 0, M encounters a blank (instead of 0). This means that the n 0’sin the right group of 0’s have all been changed to 1’s, and the first n + 1 0’s have beenchanged to B. At this point, how many 0’s are on the tape? Since n+ 1 0’s among m arechanged to B, there are m� (n+1) = m�n� 1 0’s on the tape. So, the machine replacesn+ 1 1’s by one 0 and n B’s. Therefore, m� n 0’s are on the tape.

2. Beginning the cycle, M cannot find a 0. This means that the first group of 0’s have beenchanged to B. This again means that n � m, so m n = 0. M replaces all remaining 1’sand 0’s by B and ends with a completely blank tape.

Example 32. Design a Turing machine that accepts 01⇤ + 10⇤.

M = ({q0, q1}⇥ {0, 1, B}, {0, 1}, {0, 1, B}, �, (q0, B), B, {(q1, B)})

To simplify the design, we use a trick here. We define the state of the Turing machine as thecartesian product of the ordinary states and tape symbols. Initial state is (q0, B) and (q1, B) isthe final state. Then, the transition function is defined very simply as follows:

1. �((q0, B), a) = ((q1, a), a, R) for a = 0 or a = 12. �((q1, a), a) = ((q1, a), a, R)3. �((q1, a), B) = ((q1, B), B,R)

Page 75: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

… B 0 1 1 1 B B B …

q1

0

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes f(m,n).

f(m,n) = max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B, {q6})

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

Principles (how it works):

– M repeats the following steps:1. M finds the leftmost remaining 0 and replaces it by a blank.2. It then searches right, looking for 1.3. After finding 1, it continues right, until it comes to a 0, and replaces it by 1.4. M then returns left, seeking the leftmost 0, which it identifies when it first meets a blank

and then moves one cell to the right. Now, M goes to the beginning of the cycle.– This repetition ends if either:

1. Searching right for a 0, M encounters a blank (instead of 0). This means that the n 0’sin the right group of 0’s have all been changed to 1’s, and the first n + 1 0’s have beenchanged to B. At this point, how many 0’s are on the tape? Since n+ 1 0’s among m arechanged to B, there are m� (n+1) = m�n� 1 0’s on the tape. So, the machine replacesn+ 1 1’s by one 0 and n B’s. Therefore, m� n 0’s are on the tape.

2. Beginning the cycle, M cannot find a 0. This means that the first group of 0’s have beenchanged to B. This again means that n � m, so m n = 0. M replaces all remaining 1’sand 0’s by B and ends with a completely blank tape.

Example 32. Design a Turing machine that accepts 01⇤ + 10⇤.

M = ({q0, q1}⇥ {0, 1, B}, {0, 1}, {0, 1, B}, �, (q0, B), B, {(q1, B)})

To simplify the design, we use a trick here. We define the state of the Turing machine as thecartesian product of the ordinary states and tape symbols. Initial state is (q0, B) and (q1, B) isthe final state. Then, the transition function is defined very simply as follows:

1. �((q0, B), a) = ((q1, a), a, R) for a = 0 or a = 12. �((q1, a), a) = ((q1, a), a, R)3. �((q1, a), B) = ((q1, B), B,R)

Page 76: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

… B 0 1 1 1 B B B …

q1

B

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes f(m,n).

f(m,n) = max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B, {q6})

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

Principles (how it works):

– M repeats the following steps:1. M finds the leftmost remaining 0 and replaces it by a blank.2. It then searches right, looking for 1.3. After finding 1, it continues right, until it comes to a 0, and replaces it by 1.4. M then returns left, seeking the leftmost 0, which it identifies when it first meets a blank

and then moves one cell to the right. Now, M goes to the beginning of the cycle.– This repetition ends if either:

1. Searching right for a 0, M encounters a blank (instead of 0). This means that the n 0’sin the right group of 0’s have all been changed to 1’s, and the first n + 1 0’s have beenchanged to B. At this point, how many 0’s are on the tape? Since n+ 1 0’s among m arechanged to B, there are m� (n+1) = m�n� 1 0’s on the tape. So, the machine replacesn+ 1 1’s by one 0 and n B’s. Therefore, m� n 0’s are on the tape.

2. Beginning the cycle, M cannot find a 0. This means that the first group of 0’s have beenchanged to B. This again means that n � m, so m n = 0. M replaces all remaining 1’sand 0’s by B and ends with a completely blank tape.

Example 32. Design a Turing machine that accepts 01⇤ + 10⇤.

M = ({q0, q1}⇥ {0, 1, B}, {0, 1}, {0, 1, B}, �, (q0, B), B, {(q1, B)})

To simplify the design, we use a trick here. We define the state of the Turing machine as thecartesian product of the ordinary states and tape symbols. Initial state is (q0, B) and (q1, B) isthe final state. Then, the transition function is defined very simply as follows:

1. �((q0, B), a) = ((q1, a), a, R) for a = 0 or a = 12. �((q1, a), a) = ((q1, a), a, R)3. �((q1, a), B) = ((q1, B), B,R)

Page 77: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

… B 1 0 0 0 B B B …

q0

B

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes f(m,n).

f(m,n) = max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B, {q6})

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

Principles (how it works):

– M repeats the following steps:1. M finds the leftmost remaining 0 and replaces it by a blank.2. It then searches right, looking for 1.3. After finding 1, it continues right, until it comes to a 0, and replaces it by 1.4. M then returns left, seeking the leftmost 0, which it identifies when it first meets a blank

and then moves one cell to the right. Now, M goes to the beginning of the cycle.– This repetition ends if either:

1. Searching right for a 0, M encounters a blank (instead of 0). This means that the n 0’sin the right group of 0’s have all been changed to 1’s, and the first n + 1 0’s have beenchanged to B. At this point, how many 0’s are on the tape? Since n+ 1 0’s among m arechanged to B, there are m� (n+1) = m�n� 1 0’s on the tape. So, the machine replacesn+ 1 1’s by one 0 and n B’s. Therefore, m� n 0’s are on the tape.

2. Beginning the cycle, M cannot find a 0. This means that the first group of 0’s have beenchanged to B. This again means that n � m, so m n = 0. M replaces all remaining 1’sand 0’s by B and ends with a completely blank tape.

Example 32. Design a Turing machine that accepts 01⇤ + 10⇤.

M = ({q0, q1}⇥ {0, 1, B}, {0, 1}, {0, 1, B}, �, (q0, B), B, {(q1, B)})

To simplify the design, we use a trick here. We define the state of the Turing machine as thecartesian product of the ordinary states and tape symbols. Initial state is (q0, B) and (q1, B) isthe final state. Then, the transition function is defined very simply as follows:

1. �((q0, B), a) = ((q1, a), a, R) for a = 0 or a = 12. �((q1, a), a) = ((q1, a), a, R)3. �((q1, a), B) = ((q1, B), B,R)

Page 78: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

q1

1

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes f(m,n).

f(m,n) = max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B, {q6})

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

Principles (how it works):

– M repeats the following steps:1. M finds the leftmost remaining 0 and replaces it by a blank.2. It then searches right, looking for 1.3. After finding 1, it continues right, until it comes to a 0, and replaces it by 1.4. M then returns left, seeking the leftmost 0, which it identifies when it first meets a blank

and then moves one cell to the right. Now, M goes to the beginning of the cycle.– This repetition ends if either:

1. Searching right for a 0, M encounters a blank (instead of 0). This means that the n 0’sin the right group of 0’s have all been changed to 1’s, and the first n + 1 0’s have beenchanged to B. At this point, how many 0’s are on the tape? Since n+ 1 0’s among m arechanged to B, there are m� (n+1) = m�n� 1 0’s on the tape. So, the machine replacesn+ 1 1’s by one 0 and n B’s. Therefore, m� n 0’s are on the tape.

2. Beginning the cycle, M cannot find a 0. This means that the first group of 0’s have beenchanged to B. This again means that n � m, so m n = 0. M replaces all remaining 1’sand 0’s by B and ends with a completely blank tape.

Example 32. Design a Turing machine that accepts 01⇤ + 10⇤.

M = ({q0, q1}⇥ {0, 1, B}, {0, 1}, {0, 1, B}, �, (q0, B), B, {(q1, B)})

To simplify the design, we use a trick here. We define the state of the Turing machine as thecartesian product of the ordinary states and tape symbols. Initial state is (q0, B) and (q1, B) isthe final state. Then, the transition function is defined very simply as follows:

1. �((q0, B), a) = ((q1, a), a, R) for a = 0 or a = 12. �((q1, a), a) = ((q1, a), a, R)3. �((q1, a), B) = ((q1, B), B,R)

… B 1 0 0 0 B B B …

Page 79: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

q1

1

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes f(m,n).

f(m,n) = max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B, {q6})

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

Principles (how it works):

– M repeats the following steps:1. M finds the leftmost remaining 0 and replaces it by a blank.2. It then searches right, looking for 1.3. After finding 1, it continues right, until it comes to a 0, and replaces it by 1.4. M then returns left, seeking the leftmost 0, which it identifies when it first meets a blank

and then moves one cell to the right. Now, M goes to the beginning of the cycle.– This repetition ends if either:

1. Searching right for a 0, M encounters a blank (instead of 0). This means that the n 0’sin the right group of 0’s have all been changed to 1’s, and the first n + 1 0’s have beenchanged to B. At this point, how many 0’s are on the tape? Since n+ 1 0’s among m arechanged to B, there are m� (n+1) = m�n� 1 0’s on the tape. So, the machine replacesn+ 1 1’s by one 0 and n B’s. Therefore, m� n 0’s are on the tape.

2. Beginning the cycle, M cannot find a 0. This means that the first group of 0’s have beenchanged to B. This again means that n � m, so m n = 0. M replaces all remaining 1’sand 0’s by B and ends with a completely blank tape.

Example 32. Design a Turing machine that accepts 01⇤ + 10⇤.

M = ({q0, q1}⇥ {0, 1, B}, {0, 1}, {0, 1, B}, �, (q0, B), B, {(q1, B)})

To simplify the design, we use a trick here. We define the state of the Turing machine as thecartesian product of the ordinary states and tape symbols. Initial state is (q0, B) and (q1, B) isthe final state. Then, the transition function is defined very simply as follows:

1. �((q0, B), a) = ((q1, a), a, R) for a = 0 or a = 12. �((q1, a), a) = ((q1, a), a, R)3. �((q1, a), B) = ((q1, B), B,R)

… B 1 0 0 0 B B B …

Page 80: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

q1

1

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes f(m,n).

f(m,n) = max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B, {q6})

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

Principles (how it works):

– M repeats the following steps:1. M finds the leftmost remaining 0 and replaces it by a blank.2. It then searches right, looking for 1.3. After finding 1, it continues right, until it comes to a 0, and replaces it by 1.4. M then returns left, seeking the leftmost 0, which it identifies when it first meets a blank

and then moves one cell to the right. Now, M goes to the beginning of the cycle.– This repetition ends if either:

1. Searching right for a 0, M encounters a blank (instead of 0). This means that the n 0’sin the right group of 0’s have all been changed to 1’s, and the first n + 1 0’s have beenchanged to B. At this point, how many 0’s are on the tape? Since n+ 1 0’s among m arechanged to B, there are m� (n+1) = m�n� 1 0’s on the tape. So, the machine replacesn+ 1 1’s by one 0 and n B’s. Therefore, m� n 0’s are on the tape.

2. Beginning the cycle, M cannot find a 0. This means that the first group of 0’s have beenchanged to B. This again means that n � m, so m n = 0. M replaces all remaining 1’sand 0’s by B and ends with a completely blank tape.

Example 32. Design a Turing machine that accepts 01⇤ + 10⇤.

M = ({q0, q1}⇥ {0, 1, B}, {0, 1}, {0, 1, B}, �, (q0, B), B, {(q1, B)})

To simplify the design, we use a trick here. We define the state of the Turing machine as thecartesian product of the ordinary states and tape symbols. Initial state is (q0, B) and (q1, B) isthe final state. Then, the transition function is defined very simply as follows:

1. �((q0, B), a) = ((q1, a), a, R) for a = 0 or a = 12. �((q1, a), a) = ((q1, a), a, R)3. �((q1, a), B) = ((q1, B), B,R)

… B 1 0 0 0 B B B …

Page 81: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

q1

1

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes f(m,n).

f(m,n) = max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B, {q6})

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

Principles (how it works):

– M repeats the following steps:1. M finds the leftmost remaining 0 and replaces it by a blank.2. It then searches right, looking for 1.3. After finding 1, it continues right, until it comes to a 0, and replaces it by 1.4. M then returns left, seeking the leftmost 0, which it identifies when it first meets a blank

and then moves one cell to the right. Now, M goes to the beginning of the cycle.– This repetition ends if either:

1. Searching right for a 0, M encounters a blank (instead of 0). This means that the n 0’sin the right group of 0’s have all been changed to 1’s, and the first n + 1 0’s have beenchanged to B. At this point, how many 0’s are on the tape? Since n+ 1 0’s among m arechanged to B, there are m� (n+1) = m�n� 1 0’s on the tape. So, the machine replacesn+ 1 1’s by one 0 and n B’s. Therefore, m� n 0’s are on the tape.

2. Beginning the cycle, M cannot find a 0. This means that the first group of 0’s have beenchanged to B. This again means that n � m, so m n = 0. M replaces all remaining 1’sand 0’s by B and ends with a completely blank tape.

Example 32. Design a Turing machine that accepts 01⇤ + 10⇤.

M = ({q0, q1}⇥ {0, 1, B}, {0, 1}, {0, 1, B}, �, (q0, B), B, {(q1, B)})

To simplify the design, we use a trick here. We define the state of the Turing machine as thecartesian product of the ordinary states and tape symbols. Initial state is (q0, B) and (q1, B) isthe final state. Then, the transition function is defined very simply as follows:

1. �((q0, B), a) = ((q1, a), a, R) for a = 0 or a = 12. �((q1, a), a) = ((q1, a), a, R)3. �((q1, a), B) = ((q1, B), B,R)

… B 1 0 0 0 B B B …

Page 82: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

q1

B

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes f(m,n).

f(m,n) = max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B, {q6})

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

Principles (how it works):

– M repeats the following steps:1. M finds the leftmost remaining 0 and replaces it by a blank.2. It then searches right, looking for 1.3. After finding 1, it continues right, until it comes to a 0, and replaces it by 1.4. M then returns left, seeking the leftmost 0, which it identifies when it first meets a blank

and then moves one cell to the right. Now, M goes to the beginning of the cycle.– This repetition ends if either:

1. Searching right for a 0, M encounters a blank (instead of 0). This means that the n 0’sin the right group of 0’s have all been changed to 1’s, and the first n + 1 0’s have beenchanged to B. At this point, how many 0’s are on the tape? Since n+ 1 0’s among m arechanged to B, there are m� (n+1) = m�n� 1 0’s on the tape. So, the machine replacesn+ 1 1’s by one 0 and n B’s. Therefore, m� n 0’s are on the tape.

2. Beginning the cycle, M cannot find a 0. This means that the first group of 0’s have beenchanged to B. This again means that n � m, so m n = 0. M replaces all remaining 1’sand 0’s by B and ends with a completely blank tape.

Example 32. Design a Turing machine that accepts 01⇤ + 10⇤.

M = ({q0, q1}⇥ {0, 1, B}, {0, 1}, {0, 1, B}, �, (q0, B), B, {(q1, B)})

To simplify the design, we use a trick here. We define the state of the Turing machine as thecartesian product of the ordinary states and tape symbols. Initial state is (q0, B) and (q1, B) isthe final state. Then, the transition function is defined very simply as follows:

1. �((q0, B), a) = ((q1, a), a, R) for a = 0 or a = 12. �((q1, a), a) = ((q1, a), a, R)3. �((q1, a), B) = ((q1, B), B,R)

… B 1 0 0 0 B B B …

Page 83: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

Example 6.

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes f(m,n).

f(m,n) = max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B, {q6})

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

Principles (how it works):

– M repeats the following steps:1. M finds the leftmost remaining 0 and replaces it by a blank.2. It then searches right, looking for 1.3. After finding 1, it continues right, until it comes to a 0, and replaces it by 1.4. M then returns left, seeking the leftmost 0, which it identifies when it first meets a blank

and then moves one cell to the right. Now, M goes to the beginning of the cycle.– This repetition ends if either:

1. Searching right for a 0, M encounters a blank (instead of 0). This means that the n 0’sin the right group of 0’s have all been changed to 1’s, and the first n + 1 0’s have beenchanged to B. At this point, how many 0’s are on the tape? Since n+ 1 0’s among m arechanged to B, there are m� (n+1) = m�n� 1 0’s on the tape. So, the machine replacesn+ 1 1’s by one 0 and n B’s. Therefore, m� n 0’s are on the tape.

2. Beginning the cycle, M cannot find a 0. This means that the first group of 0’s have beenchanged to B. This again means that n � m, so m n = 0. M replaces all remaining 1’sand 0’s by B and ends with a completely blank tape.

Example 32. Design a Turing machine that accepts 01⇤ + 10⇤.

M = ({q0, q1}⇥ {0, 1, B}, {0, 1}, {0, 1, B}, �, (q0, B), B, {(q1, B)})

To simplify the design, we use a trick here. We define the state of the Turing machine as thecartesian product of the ordinary states and tape symbols. Initial state is (q0, B) and (q1, B) isthe final state. Then, the transition function is defined very simply as follows:

1. �((q0, B), a) = ((q1, a), a, R) for a = 0 or a = 12. �((q1, a), a) = ((q1, a), a, R)3. �((q1, a), B) = ((q1, B), B,R)

Example 33. Design a Turing machine that accepts L = {wcw | w 2 {0, 1}+}.

26

�((q1, B), (B, a)) = ((q2, a), (⇤, a), R) �((q5, B), (B, a)) = ((q6, B), (B, a), L)�((q2, a), (B, b)) = ((q2, a), (B, b), R) �((q6, B), (B, a)) = ((q6, B), (B, a), L)�((q2, a), (B, c)) = ((q3, a), (B, c), R) �((q6, B), (⇤, a)) = ((q1, B), (⇤, a), R)�((q3, a), (⇤, b)) = ((q3, a), (⇤, b), R) �((q5, B), (⇤, a)) = ((q7, B), (⇤, a), R)�((q3, a), (B, a)) = ((q4, B), (⇤, a), L) �((q7, B), (B, c)) = ((q8, B), (B, c), R)�((q4, B), (⇤, a)) = ((q4, B), (⇤, a), L) �((q8, B), (⇤, a)) = ((q8, B), (⇤, a), R)�((q4, B), (B, c)) = ((q5, B), (B, c), L) �((q8, B), (B,B)) = ((q9, B), (B,B), R)

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes f(m,n).

f(m,n) = max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B, {q6})

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

Principles (how it works):

– M repeats the following steps:1. M finds the leftmost remaining 0 and replaces it by a blank.2. It then searches right, looking for 1.3. After finding 1, it continues right, until it comes to a 0, and replaces it by 1.4. M then returns left, seeking the leftmost 0, which it identifies when it first meets a blank

and then moves one cell to the right. Now, M goes to the beginning of the cycle.– This repetition ends if either:

1. Searching right for a 0, M encounters a blank (instead of 0). This means that the n 0’sin the right group of 0’s have all been changed to 1’s, and the first n + 1 0’s have beenchanged to B. At this point, how many 0’s are on the tape? Since n+ 1 0’s among m arechanged to B, there are m� (n+1) = m�n� 1 0’s on the tape. So, the machine replacesn+ 1 1’s by one 0 and n B’s. Therefore, m� n 0’s are on the tape.

2. Beginning the cycle, M cannot find a 0. This means that the first group of 0’s have beenchanged to B. This again means that n � m, so m n = 0. M replaces all remaining 1’sand 0’s by B and ends with a completely blank tape.

Example 32. Design a Turing machine that accepts 01⇤ + 10⇤.

M = ({q0, q1}⇥ {0, 1, B}, {0, 1}, {0, 1, B}, �, (q0, B), B, {(q1, B)})

To simplify the design, we use a trick here. We define the state of the Turing machine as thecartesian product of the ordinary states and tape symbols. Initial state is (q0, B) and (q1, B) isthe final state. Then, the transition function is defined very simply as follows:

1. �((q0, B), a) = ((q1, a), a, R) for a = 0 or a = 12. �((q1, a), a) = ((q1, a), a, R)3. �((q1, a), B) = ((q1, B), B,R)

Example 33. Design a Turing machine that accepts L = {wcw | w 2 {0, 1}+}.

M = (Q,⌃,�, �, (q1, B), (B,B), {q9, B})

– {q1, q2, . . . , q9}⇥ {0, 1, B}– � = {B, ⇤}⇥ {0, 1, c, B}– ⌃ = {(B, 0), (B, 1), (B, c)}

25

4.3 Programming Techniques for Turing Machines

Example 31 (Proper Subtraction). Design a Turing machine that computes f(m,n).

f(m,n) = max(m� n, 0) = if m � n then m� n else 0

M = ({q0, q1, . . . , q6}, {0, 1}, {0, 1, B}, �, q0, B, {q6})

Note that we have omitted the final states. The machine starts with 1m01n and halts with1max(m�n,0) on its tape.

0 1 B

q0 (q1, B,R) (q5, B,R)q1 (q1, 0, R) (q2, 1, R)q2 (q3, 1, L) (q2, 1, R) (q4, B, L)q3 (q3, 0, L) (q3, 1, L) (q0, B,R)q4 (q4, 0, L) (q4, B, L) (q6, 0, R)q5 (q5, B,R) (q5, B,R) (q6, B,R)q6

Principles (how it works):

– M repeats the following steps:1. M finds the leftmost remaining 0 and replaces it by a blank.2. It then searches right, looking for 1.3. After finding 1, it continues right, until it comes to a 0, and replaces it by 1.4. M then returns left, seeking the leftmost 0, which it identifies when it first meets a blank

and then moves one cell to the right. Now, M goes to the beginning of the cycle.– This repetition ends if either:

1. Searching right for a 0, M encounters a blank (instead of 0). This means that the n 0’sin the right group of 0’s have all been changed to 1’s, and the first n + 1 0’s have beenchanged to B. At this point, how many 0’s are on the tape? Since n+ 1 0’s among m arechanged to B, there are m� (n+1) = m�n� 1 0’s on the tape. So, the machine replacesn+ 1 1’s by one 0 and n B’s. Therefore, m� n 0’s are on the tape.

2. Beginning the cycle, M cannot find a 0. This means that the first group of 0’s have beenchanged to B. This again means that n � m, so m n = 0. M replaces all remaining 1’sand 0’s by B and ends with a completely blank tape.

Example 32. Design a Turing machine that accepts 01⇤ + 10⇤.

M = ({q0, q1}⇥ {0, 1, B}, {0, 1}, {0, 1, B}, �, (q0, B), B, {(q1, B)})

To simplify the design, we use a trick here. We define the state of the Turing machine as thecartesian product of the ordinary states and tape symbols. Initial state is (q0, B) and (q1, B) isthe final state. Then, the transition function is defined very simply as follows:

1. �((q0, B), a) = ((q1, a), a, R) for a = 0 or a = 12. �((q1, a), a) = ((q1, a), a, R)3. �((q1, a), B) = ((q1, B), B,R)

Example 33. Design a Turing machine that accepts L = {wcw | w 2 {0, 1}+}.

M = (Q,⌃,�, �, (q1, B), (B,B), {q9, B})

– {q1, q2, . . . , q9}⇥ {0, 1, B}– � = {B, ⇤}⇥ {0, 1, c, B}– ⌃ = {(B, 0), (B, 1), (B, c)}

Page 84: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

… B B B B B B B B …

q1

B

… B 0 1 c 0 1 B B …

26

�((q1, B), (B, a)) = ((q2, a), (⇤, a), R) �((q5, B), (B, a)) = ((q6, B), (B, a), L)�((q2, a), (B, b)) = ((q2, a), (B, b), R) �((q6, B), (B, a)) = ((q6, B), (B, a), L)�((q2, a), (B, c)) = ((q3, a), (B, c), R) �((q6, B), (⇤, a)) = ((q1, B), (⇤, a), R)�((q3, a), (⇤, b)) = ((q3, a), (⇤, b), R) �((q5, B), (⇤, a)) = ((q7, B), (⇤, a), R)�((q3, a), (B, a)) = ((q4, B), (⇤, a), L) �((q7, B), (B, c)) = ((q8, B), (B, c), R)�((q4, B), (⇤, a)) = ((q4, B), (⇤, a), L) �((q8, B), (⇤, a)) = ((q8, B), (⇤, a), R)�((q4, B), (B, c)) = ((q5, B), (B, c), L) �((q8, B), (B,B)) = ((q9, B), (B,B), R)

Page 85: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

… B * B B B B B B …

q2

0

… B 0 1 c 0 1 B B …

26

�((q1, B), (B, a)) = ((q2, a), (⇤, a), R) �((q5, B), (B, a)) = ((q6, B), (B, a), L)�((q2, a), (B, b)) = ((q2, a), (B, b), R) �((q6, B), (B, a)) = ((q6, B), (B, a), L)�((q2, a), (B, c)) = ((q3, a), (B, c), R) �((q6, B), (⇤, a)) = ((q1, B), (⇤, a), R)�((q3, a), (⇤, b)) = ((q3, a), (⇤, b), R) �((q5, B), (⇤, a)) = ((q7, B), (⇤, a), R)�((q3, a), (B, a)) = ((q4, B), (⇤, a), L) �((q7, B), (B, c)) = ((q8, B), (B, c), R)�((q4, B), (⇤, a)) = ((q4, B), (⇤, a), L) �((q8, B), (⇤, a)) = ((q8, B), (⇤, a), R)�((q4, B), (B, c)) = ((q5, B), (B, c), L) �((q8, B), (B,B)) = ((q9, B), (B,B), R)

In q2, moves right, looking for c

Page 86: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

… B * B B B B B B …

q2

0

… B 0 1 c 0 1 B B …

26

�((q1, B), (B, a)) = ((q2, a), (⇤, a), R) �((q5, B), (B, a)) = ((q6, B), (B, a), L)�((q2, a), (B, b)) = ((q2, a), (B, b), R) �((q6, B), (B, a)) = ((q6, B), (B, a), L)�((q2, a), (B, c)) = ((q3, a), (B, c), R) �((q6, B), (⇤, a)) = ((q1, B), (⇤, a), R)�((q3, a), (⇤, b)) = ((q3, a), (⇤, b), R) �((q5, B), (⇤, a)) = ((q7, B), (⇤, a), R)�((q3, a), (B, a)) = ((q4, B), (⇤, a), L) �((q7, B), (B, c)) = ((q8, B), (B, c), R)�((q4, B), (⇤, a)) = ((q4, B), (⇤, a), L) �((q8, B), (⇤, a)) = ((q8, B), (⇤, a), R)�((q4, B), (B, c)) = ((q5, B), (B, c), L) �((q8, B), (B,B)) = ((q9, B), (B,B), R)

When found, - enter q3- continue right

Page 87: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

… B * B B B B B B …

q3

0

… B 0 1 c 0 1 B B …

26

�((q1, B), (B, a)) = ((q2, a), (⇤, a), R) �((q5, B), (B, a)) = ((q6, B), (B, a), L)�((q2, a), (B, b)) = ((q2, a), (B, b), R) �((q6, B), (B, a)) = ((q6, B), (B, a), L)�((q2, a), (B, c)) = ((q3, a), (B, c), R) �((q6, B), (⇤, a)) = ((q1, B), (⇤, a), R)�((q3, a), (⇤, b)) = ((q3, a), (⇤, b), R) �((q5, B), (⇤, a)) = ((q7, B), (⇤, a), R)�((q3, a), (B, a)) = ((q4, B), (⇤, a), L) �((q7, B), (B, c)) = ((q8, B), (B, c), R)�((q4, B), (⇤, a)) = ((q4, B), (⇤, a), L) �((q8, B), (⇤, a)) = ((q8, B), (⇤, a), R)�((q4, B), (B, c)) = ((q5, B), (B, c), L) �((q8, B), (B,B)) = ((q9, B), (B,B), R)

In q3, look for the first unchecked symbol

Page 88: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

… B * B B * B B B …

q4

B

… B 0 1 c 0 1 B B …

26

�((q1, B), (B, a)) = ((q2, a), (⇤, a), R) �((q5, B), (B, a)) = ((q6, B), (B, a), L)�((q2, a), (B, b)) = ((q2, a), (B, b), R) �((q6, B), (B, a)) = ((q6, B), (B, a), L)�((q2, a), (B, c)) = ((q3, a), (B, c), R) �((q6, B), (⇤, a)) = ((q1, B), (⇤, a), R)�((q3, a), (⇤, b)) = ((q3, a), (⇤, b), R) �((q5, B), (⇤, a)) = ((q7, B), (⇤, a), R)�((q3, a), (B, a)) = ((q4, B), (⇤, a), L) �((q7, B), (B, c)) = ((q8, B), (B, c), R)�((q4, B), (⇤, a)) = ((q4, B), (⇤, a), L) �((q8, B), (⇤, a)) = ((q8, B), (⇤, a), R)�((q4, B), (B, c)) = ((q5, B), (B, c), L) �((q8, B), (B,B)) = ((q9, B), (B,B), R)

- In q4, move left until it finds c- When found, enter q5

Page 89: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

… B * B B * B B B …

q5

B

… B 0 1 c 0 1 B B …

26

�((q1, B), (B, a)) = ((q2, a), (⇤, a), R) �((q5, B), (B, a)) = ((q6, B), (B, a), L)�((q2, a), (B, b)) = ((q2, a), (B, b), R) �((q6, B), (B, a)) = ((q6, B), (B, a), L)�((q2, a), (B, c)) = ((q3, a), (B, c), R) �((q6, B), (⇤, a)) = ((q1, B), (⇤, a), R)�((q3, a), (⇤, b)) = ((q3, a), (⇤, b), R) �((q5, B), (⇤, a)) = ((q7, B), (⇤, a), R)�((q3, a), (B, a)) = ((q4, B), (⇤, a), L) �((q7, B), (B, c)) = ((q8, B), (B, c), R)�((q4, B), (⇤, a)) = ((q4, B), (⇤, a), L) �((q8, B), (⇤, a)) = ((q8, B), (⇤, a), R)�((q4, B), (B, c)) = ((q5, B), (B, c), L) �((q8, B), (B,B)) = ((q9, B), (B,B), R)

- If there is some unchecked symbol, enter q6

Page 90: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

… B * B B * B B B …

q6

B

… B 0 1 c 0 1 B B …

26

�((q1, B), (B, a)) = ((q2, a), (⇤, a), R) �((q5, B), (B, a)) = ((q6, B), (B, a), L)�((q2, a), (B, b)) = ((q2, a), (B, b), R) �((q6, B), (B, a)) = ((q6, B), (B, a), L)�((q2, a), (B, c)) = ((q3, a), (B, c), R) �((q6, B), (⇤, a)) = ((q1, B), (⇤, a), R)�((q3, a), (⇤, b)) = ((q3, a), (⇤, b), R) �((q5, B), (⇤, a)) = ((q7, B), (⇤, a), R)�((q3, a), (B, a)) = ((q4, B), (⇤, a), L) �((q7, B), (B, c)) = ((q8, B), (B, c), R)�((q4, B), (⇤, a)) = ((q4, B), (⇤, a), L) �((q8, B), (⇤, a)) = ((q8, B), (⇤, a), R)�((q4, B), (B, c)) = ((q5, B), (B, c), L) �((q8, B), (B,B)) = ((q9, B), (B,B), R)

- In q6, move left and look for the first unchecked symbol

Page 91: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

… B * B B * B B B …

q1

B

… B 0 1 c 0 1 B B …

26

�((q1, B), (B, a)) = ((q2, a), (⇤, a), R) �((q5, B), (B, a)) = ((q6, B), (B, a), L)�((q2, a), (B, b)) = ((q2, a), (B, b), R) �((q6, B), (B, a)) = ((q6, B), (B, a), L)�((q2, a), (B, c)) = ((q3, a), (B, c), R) �((q6, B), (⇤, a)) = ((q1, B), (⇤, a), R)�((q3, a), (⇤, b)) = ((q3, a), (⇤, b), R) �((q5, B), (⇤, a)) = ((q7, B), (⇤, a), R)�((q3, a), (B, a)) = ((q4, B), (⇤, a), L) �((q7, B), (B, c)) = ((q8, B), (B, c), R)�((q4, B), (⇤, a)) = ((q4, B), (⇤, a), L) �((q8, B), (⇤, a)) = ((q8, B), (⇤, a), R)�((q4, B), (B, c)) = ((q5, B), (B, c), L) �((q8, B), (B,B)) = ((q9, B), (B,B), R)

- Repeat the cycle

Page 92: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

… B * * B * B B B …

q2

1

… B 0 1 c 0 1 B B …

26

�((q1, B), (B, a)) = ((q2, a), (⇤, a), R) �((q5, B), (B, a)) = ((q6, B), (B, a), L)�((q2, a), (B, b)) = ((q2, a), (B, b), R) �((q6, B), (B, a)) = ((q6, B), (B, a), L)�((q2, a), (B, c)) = ((q3, a), (B, c), R) �((q6, B), (⇤, a)) = ((q1, B), (⇤, a), R)�((q3, a), (⇤, b)) = ((q3, a), (⇤, b), R) �((q5, B), (⇤, a)) = ((q7, B), (⇤, a), R)�((q3, a), (B, a)) = ((q4, B), (⇤, a), L) �((q7, B), (B, c)) = ((q8, B), (B, c), R)�((q4, B), (⇤, a)) = ((q4, B), (⇤, a), L) �((q8, B), (⇤, a)) = ((q8, B), (⇤, a), R)�((q4, B), (B, c)) = ((q5, B), (B, c), L) �((q8, B), (B,B)) = ((q9, B), (B,B), R)

Page 93: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

… B * * B * B B B …

q3

1

… B 0 1 c 0 1 B B …

26

�((q1, B), (B, a)) = ((q2, a), (⇤, a), R) �((q5, B), (B, a)) = ((q6, B), (B, a), L)�((q2, a), (B, b)) = ((q2, a), (B, b), R) �((q6, B), (B, a)) = ((q6, B), (B, a), L)�((q2, a), (B, c)) = ((q3, a), (B, c), R) �((q6, B), (⇤, a)) = ((q1, B), (⇤, a), R)�((q3, a), (⇤, b)) = ((q3, a), (⇤, b), R) �((q5, B), (⇤, a)) = ((q7, B), (⇤, a), R)�((q3, a), (B, a)) = ((q4, B), (⇤, a), L) �((q7, B), (B, c)) = ((q8, B), (B, c), R)�((q4, B), (⇤, a)) = ((q4, B), (⇤, a), L) �((q8, B), (⇤, a)) = ((q8, B), (⇤, a), R)�((q4, B), (B, c)) = ((q5, B), (B, c), L) �((q8, B), (B,B)) = ((q9, B), (B,B), R)

Page 94: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

… B * * B * B B B …

q3

1

… B 0 1 c 0 1 B B …

26

�((q1, B), (B, a)) = ((q2, a), (⇤, a), R) �((q5, B), (B, a)) = ((q6, B), (B, a), L)�((q2, a), (B, b)) = ((q2, a), (B, b), R) �((q6, B), (B, a)) = ((q6, B), (B, a), L)�((q2, a), (B, c)) = ((q3, a), (B, c), R) �((q6, B), (⇤, a)) = ((q1, B), (⇤, a), R)�((q3, a), (⇤, b)) = ((q3, a), (⇤, b), R) �((q5, B), (⇤, a)) = ((q7, B), (⇤, a), R)�((q3, a), (B, a)) = ((q4, B), (⇤, a), L) �((q7, B), (B, c)) = ((q8, B), (B, c), R)�((q4, B), (⇤, a)) = ((q4, B), (⇤, a), L) �((q8, B), (⇤, a)) = ((q8, B), (⇤, a), R)�((q4, B), (B, c)) = ((q5, B), (B, c), L) �((q8, B), (B,B)) = ((q9, B), (B,B), R)

Page 95: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

… B * * B * * B B …

q4

B

… B 0 1 c 0 1 B B …

26

�((q1, B), (B, a)) = ((q2, a), (⇤, a), R) �((q5, B), (B, a)) = ((q6, B), (B, a), L)�((q2, a), (B, b)) = ((q2, a), (B, b), R) �((q6, B), (B, a)) = ((q6, B), (B, a), L)�((q2, a), (B, c)) = ((q3, a), (B, c), R) �((q6, B), (⇤, a)) = ((q1, B), (⇤, a), R)�((q3, a), (⇤, b)) = ((q3, a), (⇤, b), R) �((q5, B), (⇤, a)) = ((q7, B), (⇤, a), R)�((q3, a), (B, a)) = ((q4, B), (⇤, a), L) �((q7, B), (B, c)) = ((q8, B), (B, c), R)�((q4, B), (⇤, a)) = ((q4, B), (⇤, a), L) �((q8, B), (⇤, a)) = ((q8, B), (⇤, a), R)�((q4, B), (B, c)) = ((q5, B), (B, c), L) �((q8, B), (B,B)) = ((q9, B), (B,B), R)

Page 96: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

… B * * B * * B B …

q4

B

… B 0 1 c 0 1 B B …

26

�((q1, B), (B, a)) = ((q2, a), (⇤, a), R) �((q5, B), (B, a)) = ((q6, B), (B, a), L)�((q2, a), (B, b)) = ((q2, a), (B, b), R) �((q6, B), (B, a)) = ((q6, B), (B, a), L)�((q2, a), (B, c)) = ((q3, a), (B, c), R) �((q6, B), (⇤, a)) = ((q1, B), (⇤, a), R)�((q3, a), (⇤, b)) = ((q3, a), (⇤, b), R) �((q5, B), (⇤, a)) = ((q7, B), (⇤, a), R)�((q3, a), (B, a)) = ((q4, B), (⇤, a), L) �((q7, B), (B, c)) = ((q8, B), (B, c), R)�((q4, B), (⇤, a)) = ((q4, B), (⇤, a), L) �((q8, B), (⇤, a)) = ((q8, B), (⇤, a), R)�((q4, B), (B, c)) = ((q5, B), (B, c), L) �((q8, B), (B,B)) = ((q9, B), (B,B), R)

Page 97: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

… B * * B * * B B …

q5

B

… B 0 1 c 0 1 B B …

26

�((q1, B), (B, a)) = ((q2, a), (⇤, a), R) �((q5, B), (B, a)) = ((q6, B), (B, a), L)�((q2, a), (B, b)) = ((q2, a), (B, b), R) �((q6, B), (B, a)) = ((q6, B), (B, a), L)�((q2, a), (B, c)) = ((q3, a), (B, c), R) �((q6, B), (⇤, a)) = ((q1, B), (⇤, a), R)�((q3, a), (⇤, b)) = ((q3, a), (⇤, b), R) �((q5, B), (⇤, a)) = ((q7, B), (⇤, a), R)�((q3, a), (B, a)) = ((q4, B), (⇤, a), L) �((q7, B), (B, c)) = ((q8, B), (B, c), R)�((q4, B), (⇤, a)) = ((q4, B), (⇤, a), L) �((q8, B), (⇤, a)) = ((q8, B), (⇤, a), R)�((q4, B), (B, c)) = ((q5, B), (B, c), L) �((q8, B), (B,B)) = ((q9, B), (B,B), R)

- If every symbols are checked, move right and enter q7

Page 98: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

… B * * B * * B B …

q7

B

… B 0 1 c 0 1 B B …

26

�((q1, B), (B, a)) = ((q2, a), (⇤, a), R) �((q5, B), (B, a)) = ((q6, B), (B, a), L)�((q2, a), (B, b)) = ((q2, a), (B, b), R) �((q6, B), (B, a)) = ((q6, B), (B, a), L)�((q2, a), (B, c)) = ((q3, a), (B, c), R) �((q6, B), (⇤, a)) = ((q1, B), (⇤, a), R)�((q3, a), (⇤, b)) = ((q3, a), (⇤, b), R) �((q5, B), (⇤, a)) = ((q7, B), (⇤, a), R)�((q3, a), (B, a)) = ((q4, B), (⇤, a), L) �((q7, B), (B, c)) = ((q8, B), (B, c), R)�((q4, B), (⇤, a)) = ((q4, B), (⇤, a), L) �((q8, B), (⇤, a)) = ((q8, B), (⇤, a), R)�((q4, B), (B, c)) = ((q5, B), (B, c), L) �((q8, B), (B,B)) = ((q9, B), (B,B), R)

- If c is found, enter q8

Page 99: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

… B * * B * * B B …

q8

B

… B 0 1 c 0 1 B B …

26

�((q1, B), (B, a)) = ((q2, a), (⇤, a), R) �((q5, B), (B, a)) = ((q6, B), (B, a), L)�((q2, a), (B, b)) = ((q2, a), (B, b), R) �((q6, B), (B, a)) = ((q6, B), (B, a), L)�((q2, a), (B, c)) = ((q3, a), (B, c), R) �((q6, B), (⇤, a)) = ((q1, B), (⇤, a), R)�((q3, a), (⇤, b)) = ((q3, a), (⇤, b), R) �((q5, B), (⇤, a)) = ((q7, B), (⇤, a), R)�((q3, a), (B, a)) = ((q4, B), (⇤, a), L) �((q7, B), (B, c)) = ((q8, B), (B, c), R)�((q4, B), (⇤, a)) = ((q4, B), (⇤, a), L) �((q8, B), (⇤, a)) = ((q8, B), (⇤, a), R)�((q4, B), (B, c)) = ((q5, B), (B, c), L) �((q8, B), (B,B)) = ((q9, B), (B,B), R)

- move right until it finds B

Page 100: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

… B * * B * * B B …

q8

B

… B 0 1 c 0 1 B B …

26

�((q1, B), (B, a)) = ((q2, a), (⇤, a), R) �((q5, B), (B, a)) = ((q6, B), (B, a), L)�((q2, a), (B, b)) = ((q2, a), (B, b), R) �((q6, B), (B, a)) = ((q6, B), (B, a), L)�((q2, a), (B, c)) = ((q3, a), (B, c), R) �((q6, B), (⇤, a)) = ((q1, B), (⇤, a), R)�((q3, a), (⇤, b)) = ((q3, a), (⇤, b), R) �((q5, B), (⇤, a)) = ((q7, B), (⇤, a), R)�((q3, a), (B, a)) = ((q4, B), (⇤, a), L) �((q7, B), (B, c)) = ((q8, B), (B, c), R)�((q4, B), (⇤, a)) = ((q4, B), (⇤, a), L) �((q8, B), (⇤, a)) = ((q8, B), (⇤, a), R)�((q4, B), (B, c)) = ((q5, B), (B, c), L) �((q8, B), (B,B)) = ((q9, B), (B,B), R)

Page 101: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

… B * * B * * B B …

q8

B

… B 0 1 c 0 1 B B …

26

�((q1, B), (B, a)) = ((q2, a), (⇤, a), R) �((q5, B), (B, a)) = ((q6, B), (B, a), L)�((q2, a), (B, b)) = ((q2, a), (B, b), R) �((q6, B), (B, a)) = ((q6, B), (B, a), L)�((q2, a), (B, c)) = ((q3, a), (B, c), R) �((q6, B), (⇤, a)) = ((q1, B), (⇤, a), R)�((q3, a), (⇤, b)) = ((q3, a), (⇤, b), R) �((q5, B), (⇤, a)) = ((q7, B), (⇤, a), R)�((q3, a), (B, a)) = ((q4, B), (⇤, a), L) �((q7, B), (B, c)) = ((q8, B), (B, c), R)�((q4, B), (⇤, a)) = ((q4, B), (⇤, a), L) �((q8, B), (⇤, a)) = ((q8, B), (⇤, a), R)�((q4, B), (B, c)) = ((q5, B), (B, c), L) �((q8, B), (B,B)) = ((q9, B), (B,B), R)

Page 102: 4.3 Programming Techniques for Turing Machines Example 4 ...prl.korea.ac.kr/~pronto/home/courses/cose215/2015/slides/...Example 4. 25 4.3 Programming Techniques for Turing Machines

… B * * B * * B B …

q9

B

… B 0 1 c 0 1 B B …

26

�((q1, B), (B, a)) = ((q2, a), (⇤, a), R) �((q5, B), (B, a)) = ((q6, B), (B, a), L)�((q2, a), (B, b)) = ((q2, a), (B, b), R) �((q6, B), (B, a)) = ((q6, B), (B, a), L)�((q2, a), (B, c)) = ((q3, a), (B, c), R) �((q6, B), (⇤, a)) = ((q1, B), (⇤, a), R)�((q3, a), (⇤, b)) = ((q3, a), (⇤, b), R) �((q5, B), (⇤, a)) = ((q7, B), (⇤, a), R)�((q3, a), (B, a)) = ((q4, B), (⇤, a), L) �((q7, B), (B, c)) = ((q8, B), (B, c), R)�((q4, B), (⇤, a)) = ((q4, B), (⇤, a), L) �((q8, B), (⇤, a)) = ((q8, B), (⇤, a), R)�((q4, B), (B, c)) = ((q5, B), (B, c), L) �((q8, B), (B,B)) = ((q9, B), (B,B), R)