Вы находитесь на странице: 1из 23

BCS2213 Computer Science Theory

Assignment 2: Turing Machines


(Version 1.0)

Student ID Student Name


B02170029 Adla Fikriyah Bt Ibrahim
B01170005 Siti Syakirah Bt Ahmad Shaharuddin
B01170009 Amsa Ropini
Task 1
Create a Turing machine M2 that decides the language L1 = {w | w = w​R​, w ∈ {0, 1, 2}}.
So, ε, 0110, 2012102, 221122 are all in L1, while 112, 2221001, 201101 are all not in L1.
If the TM accepts the input, the TM should move to the rightmost of the string on the
tape, and writes a happy face :D followed by the sorted list of your student IDs on the
tape​.

a) Pseudocode:
1. Check leftmost symbol. If reading :
❏ Δ , Accept
❏ 0, 1 or 2:
➔ Write Δ, Move tapehead to the rightmost
➔ Else, Reject

2. Check the rightmost symbol.


❏ If match leftmost symbol, Write Δ. Move tapehead to the left
❏ Else, Reject

3. Repeat steps 1 and 2.

4. Finish check for all left and right symbol


❏ If match, Accept
➔ Print :D <ID><ID><ID>, Halt-Accept
❏ Else not match, Reject
➔ Print :( <ID><ID><ID>, Halt-Reject

1
BCS2113 Computer Science Theory
b) State Diagram:
The state diagram shows only include the main part of the program, and exclude the
smiley or sad face and the students id :

c) Listing of the program

; This Program to check palindrome with input 0,1,2

; Machine will start at state 0.

; State 0: read the leftmost symbol


0 0 _ r q1
0 1 _ r q2
0 2 _ r q3
0 _ _ * accept ; Empty input

; State q1, q2, q3: find the rightmost symbol


q1 _ _ l q4
q1 * * r q1

2
BCS2113 Computer Science Theory
q2 _ _ l q5
q2 * * r q2

q3 _ _ l q6
q3 * * r q3

; State q4, q5, q6: check if the rightmost symbol matches the most recently read
left-hand symbol
q4 0 _ l q7
q4 _ _ * accept
q4 * * * reject

q5 1 _ l q7
q5 _ _ * accept
q5 * * * reject

q6 2 _ l q7
q6 _ _ * accept
q6 * * * reject

; State q7, q8: return to left end of remaining input


q7 _ _ * accept
q7 * * l q8
q8 * * l q8
q8 _ _ r 0 ; Back to the beginning

; accept zone
accept * : r accept2
accept2 * D r accept3
accept3 * _ r accept4
accept4 * B r accept5
accept5 * 0 r accept6
accept6 * 1 r accept7
accept7 * 1 r accept8
accept8 * 7 r accept9
accept9 * 0 r accept10
accept10 * 0 r accept11
accept11 * 0 r accept12

3
BCS2113 Computer Science Theory
accept12 * 5 r accept13
accept13 * _ r accept14
accept14 * B r accept15
accept15 * 0 r accept16
accept16 * 2 r accept17
accept17 * 1 r accept18
accept18 * 7 r accept19
accept19 * 0 r accept20
accept20 * 0 r accept21
accept21 * 2 r accept22
accept22 * 9 r accept23
accept23 * _ r accept24
accept24 * B r accept25
accept25 * 0 r accept26
accept26 * 1 r accept27
accept27 * 1 r accept28
accept28 * 7 r accept29
accept29 * 0 r accept30
accept30 * 0 r accept31
accept31 * 0 r accept32
accept32 * 9 r accept33
accept33 * _ * halt-accept

;reject zone
reject * * r reject
reject _ : r reject2
reject2 * ( r reject3
reject3 * _ r reject4
reject4 * B r reject5
reject5 * 0 r reject6
reject6 * 1 r reject7
reject7 * 1 r reject8
reject8 * 7 r reject9
reject9 * 0 r reject10
reject10 * 0 r reject11
reject11 * 0 r reject12
reject12 * 5 r reject13

4
BCS2113 Computer Science Theory
reject13 * _ r reject14
reject14 * B r reject15
reject15 * 0 r reject16
reject16 * 1 r reject17
reject17 * 2 r reject18
reject18 * 7 r reject19
reject19 * 0 r reject20
reject20 * 0 r reject21
reject21 * 2 r reject22
reject22 * 9 r reject23
reject23 * _ r reject24
reject24 * B r reject25
reject25 * 0 r reject26
reject26 * 1 r reject27
reject27 * 1 r reject28
reject28 * 7 r reject29
reject29 * 0 r reject30
reject30 * 0 r reject31
reject31 * 0 r reject32
reject32 * 9 r reject33
reject33 * _ * halt-reject

d) Link to Turing Machine Simulator:


http://morphett.info/turing/turing.html?24af7a15f839d392f9aa9858ab5cd061

5
BCS2113 Computer Science Theory
Task 2
Create a Turing machine M2 that decides the language L2 = {1​n​2​2n​3​3n ​| n > 0}.
So, ε , 122333, 112222333333, 111222222333333333 are all in L2, while 321, 321313,
1122233333 are all not in L2. Similar to Turing Machine M1, depending on whether it
accepts or rejects a string, M2 will write a happy face or a bad face, followed by the sort list
of your student IDs.

a) Pseudocode:
1. Read leftmost symbol
❏ If empty, Accept
❏ If 1, change to X, and move the tapehead to right
❏ If y, move to the right, state F8
❏ Else, Reject

2. Check next symbol:


❏ If 2, change to y, and move the tapehead to the right
❏ If 1 or y, loop and move to the right
❏ Else, Reject

3. Check the next symbol:


❏ If 2, change to y, and move the tapehead to the right
❏ Else, Reject

4. Check the next symbol:


❏ If 3, change to z, and move the tapehead to the right
❏ If 2 or z, loop and move to the right
❏ Else, Reject

5. Check the next symbol:


❏ If 3, change to z, and move the tapehead to the right
❏ Else, Reject

6. Check the next symbol:


❏ If 3, change to z, and move the tapehead to the right
❏ Else, Reject

7. Check the next symbol:


❏ If 3, move the tapehead to the left
❏ Else, Reject
6
BCS2113 Computer Science Theory
8. Check the next symbol:
❏ If 3, change to z, and move the tapehead to the right
❏ Else, Reject

9. Check the next symbol:


❏ If x, move to the right and go to number 1
❏ If 1,2,y or z, loop and move to the left
❏ Else, Reject

10. Check the next symbol:


❏ If y and z, loop and move to the right
❏ If empty, move to the left and Accept
11. Finish checking for all input
❏ If Accept
➔ Print <output string> :D <ID><ID><ID>, Halt-Accept
❏ Else, Reject
➔ Print <output string> :( <ID><ID><ID>, Halt-Reject

b) State diagram:
The state diagram shows only include the main part of the program, and exclude the
smiley or sad face and the students id :

7
BCS2113 Computer Science Theory
c) Listing of the program

; Machine starts in state 0.

; State 0: read the leftmost symbol


0 1 x r F1
0 y * r F8
0 2 2 r reject
0 3 3 r reject
0 * * r reject
0 _ _ l accept ; Empty input

; State F1: find the first set of 2


F1 * * r F1
F1 _ _ r reject
F1 2 y r F2
F2 1 1 r reject
F2 3 3 r reject
F2 2 y r F3

; State F3: find the first set of 3


F3 * * r F3
F3 1 1 r reject
F3 3 z r F4
F4 1 1 r reject
F4 2 2 r reject
F4 3 z r F5
F5 1 1 r reject
F5 2 2 r reject
F5 3 z r F6

; State F6: move left if find next 3


F6 _ _ r accept
F6 3 * l F7
F6 1 1 * reject
F6 2 2 * reject

F7 * * l F7
F7 x * r 0
F7 1 * l F7
F7 2 * l F7
F7 3 * l F7
F7 y * l F7

8
BCS2113 Computer Science Theory
F7 z * l F7
F7 _ * r reject

;check x, y and z
F8 y * r F8
F8 z * r F8
F8 _ * l accept

* * * * reject

; accept zone
accept * : r accept2
accept2 * D r accept3
accept3 * _ r accept4
accept4 * B r accept5
accept5 * 0 r accept6
accept6 * 1 r accept7
accept7 * 1 r accept8
accept8 * 7 r accept9
accept9 * 0 r accept10
accept10 * 0 r accept11
accept11 * 0 r accept12
accept12 * 5 r accept13
accept13 * _ r accept14
accept14 * B r accept15
accept15 * 0 r accept16
accept16 * 2 r accept17
accept17 * 1 r accept18
accept18 * 7 r accept19
accept19 * 0 r accept20
accept20 * 0 r accept21
accept21 * 2 r accept22
accept22 * 9 r accept23
accept23 * _ r accept24
accept24 * B r accept25
accept25 * 0 r accept26
accept26 * 1 r accept27
accept27 * 1 r accept28
accept28 * 7 r accept29
accept29 * 0 r accept30
accept30 * 0 r accept31
accept31 * 0 r accept32
accept32 * 9 r accept33

9
BCS2113 Computer Science Theory
accept33 * _ * halt-accept

;reject zone
reject * * r reject
reject _ : r reject2
reject2 * ( r reject3
reject3 * _ r reject4
reject4 * B r reject5
reject5 * 0 r reject6
reject6 * 1 r reject7
reject7 * 1 r reject8
reject8 * 7 r reject9
reject9 * 0 r reject10
reject10 * 0 r reject11
reject11 * 0 r reject12
reject12 * 5 r reject13
reject13 * _ r reject14
reject14 * B r reject15
reject15 * 0 r reject16
reject16 * 1 r reject17
reject17 * 2 r reject18
reject18 * 7 r reject19
reject19 * 0 r reject20
reject20 * 0 r reject21
reject21 * 2 r reject22
reject22 * 9 r reject23
reject23 * _ r reject24
reject24 * B r reject25
reject25 * 0 r reject26
reject26 * 1 r reject27
reject27 * 1 r reject28
reject28 * 7 r reject29
reject29 * 0 r reject30
reject30 * 0 r reject31
reject31 * 0 r reject32
reject32 * 9 r reject33
reject33 * _ * halt-reject

d) Link to the Turing Machine Simulator:


​http://morphett.info/turing/turing.html?9d46943734af6362f9c1b6ab1d5febcc

10
BCS2113 Computer Science Theory
Task 3
Create a Turing machine M3 that decides the language L3 = {w-w | w ∈ {a, b, c, d}∗}.
(This language is similar to the “Kata Ganda” in Malay language, such as “bola-bola”,
“rajin-rajin”. Note also that w can be , so string ‘-’ should also be accepted.) So, -, a-a,
ab-ab, cdab-cdab are all in L3, while ad-b, b-aaddcbc, abcd-dcba are all not in L3. Similar
to Turing Machine M1, depending on whether it accepts or rejects a string, M3 will write a
happy face or a bad face, followed by the sort list of your student IDs.

a) Pseudocode:
1. read the leftmost symbol
a. If read string ‘a,b,c,d,-’, replace with ‘Δ’
b. Move to the next string and ignore it until ‘-’
2. After reading ‘-’
a. Check the string if it same with the ‘Δ’ in the left
b. Replace it with ‘X’
c. Else, Reject
3. Keep moving to the left until find ‘Δ’
4. Then, repeat step 1 and 5
5. Final string will be ‘-’ in the left, replace it with ‘Δ’
6. Keep moving to the right and replace all strings with ‘X’ until find ‘’ and
ACCEPT.
7. Print the :D <ID><ID><ID><ID><ID>, HALT-ACCEPT
8. ELSE, Print :( <ID> <ID> <ID> <ID> <ID>, HALT-REJECT

11
BCS2113 Computer Science Theory
b) State Diagram:
The state diagram shows only include the main part of the program, and exclude the
smiley or sad face and the students id :

c) Listing of the program

; This example program checks if the input string is kata ganda.


; Input: '-','a-a','ab-ab','cdab-cdab'

; Machine starts in state q0.

; State q0: read the leftmost symbol


0 a _ r q1
0 b _ r q4
0 c _ r q6
0 d _ r q8
0 - _ r q10
0 * * * reject

12
BCS2113 Computer Science Theory
; State q1,q2,q4,q6,q8: check a or b or c or d or empty string and replace it with blank symbol
q1 * * r q1
q4 * * r q4
q6 * * r q6
q8 * * r q8

; state q1,q2,q4,q6,q8: check '-' and move to the next state


q1 - * r q2
q4 - * r q5
q6 - * r q7
q8 - * r q9

;state q2,q5,q7,q9: replace blank symbol with 'X'. Else Reject

q2 X * r q2
q2 a X l q3
q5 X * r q5
q5 b X l q3
q7 X * r q7
q7 c X l q3
q9 X * r q9
q9 d X l q3
;q9 * * * reject3

;state q3: move to the left to the blank symbol and keep moving to the next string.
;then, move to right and check if it same with the left string(now blank symbol), if match
replace it with X. Else Reject
q3 * * l q3
q3 _ _ r 0
R1 c _ l reject
R1 a _ l reject
R1 b _ l reject
R1 d _ l reject

;
q10 X _ r q10
q10 _ * l q11
q10 * * r RE
q11 _ _ l accept

* * * * reject

AC * : r AC2

13
BCS2113 Computer Science Theory
AC2 * ) * halt-accept

RE2 * ( * halt-reject

; accept zone
accept * : r accept2
accept2 * D r accept3
accept3 * _ r accept4
accept4 * B r accept5
accept5 * 0 r accept6
accept6 * 1 r accept7
accept7 * 1 r accept8
accept8 * 7 r accept9
accept9 * 0 r accept10
accept10 * 0 r accept11
accept11 * 0 r accept12
accept12 * 5 r accept13
accept13 * _ r accept14
accept14 * B r accept15
accept15 * 0 r accept16
accept16 * 2 r accept17
accept17 * 1 r accept18
accept18 * 7 r accept19
accept19 * 0 r accept20
accept20 * 0 r accept21
accept21 * 2 r accept22
accept22 * 9 r accept23
accept23 * _ r accept24
accept24 * B r accept25
accept25 * 0 r accept26
accept26 * 1 r accept27
accept27 * 1 r accept28
accept28 * 7 r accept29
accept29 * 0 r accept30
accept30 * 0 r accept31
accept31 * 0 r accept32
accept32 * 9 r accept33
accept33 * _ * halt-accept

;reject zone
reject * * r reject
reject _ : r reject2
reject2 * ( r reject3

14
BCS2113 Computer Science Theory
reject3 * _ r reject4
reject4 * B r reject5
reject5 * 0 r reject6
reject6 * 1 r reject7
reject7 * 1 r reject8
reject8 * 7 r reject9
reject9 * 0 r reject10
reject10 * 0 r reject11
reject11 * 0 r reject12
reject12 * 5 r reject13
reject13 * _ r reject14
reject14 * B r reject15
reject15 * 0 r reject16
reject16 * 1 r reject17
reject17 * 2 r reject18
reject18 * 7 r reject19
reject19 * 0 r reject20
reject20 * 0 r reject21
reject21 * 2 r reject22
reject22 * 9 r reject23
reject23 * _ r reject24
reject24 * B r reject25
reject25 * 0 r reject26
reject26 * 1 r reject27
reject27 * 1 r reject28
reject28 * 7 r reject29
reject29 * 0 r reject30
reject30 * 0 r reject31
reject31 * 0 r reject32
reject32 * 9 r reject33
reject33 * _ * halt-reject

d) Link to the Turing Machine Simulator:


http://morphett.info/turing/turing.html?ace42c9389ae4269f84c6ec0c13e326f

15
BCS2113 Computer Science Theory
Task 4
Create a Turing machine M4 that decides the language L4 = {w | w ∈ {x, y, z}∗} i.e., a
string that consists of only x, y, and z. Moreover, M4 also acts as a transducer. If it accepts
the string, it will remove all of the zs from the string, and shift the xs and ys to the left. You
may use extra symbols in your solution, but your machine should halt with only some xs,
some ys, blanks (∆) and student IDs on the tape. Similar to Turing Machine M1, depending
on whether it accepts or rejects a string, it will write a happy face or a bad face, followed
by the sort list of your student IDs.

a) Pseudocode:

1. Check the leftmost symbol:


❏ If the symbol is x or y, stay here
❏ If the symbol is z, delete it
❏ If the symbol is other than x, y or z, Reject.
❏ Move tape head to the right

2. Shift the remaining symbols:


❏ If the symbol read blank
➔ Move the tape head to left

➔ When read z:
● Delete it
● Move tape head to right
● Repeat step 2 until end of string

➔ When read blanks:


● Accept

❏ If the symbol read non-blank


➔ Read and delete the symbol
➔ Move tape head to the left
➔ Write the previous symbol

3. Finish checking for all input


❏ If Accept
➔ Print <output string> :D <ID><ID><ID>, Halt-Accept
❏ Else, Reject
➔ Print <output string> :( <ID><ID><ID>, Halt-Reject
16
BCS2113 Computer Science Theory
b) State diagram:
The state diagram shows only include the main part of the program, and exclude the
smiley or sad face and the students id :

​c) Listing of the program

;; This example program is for task 4


; L4 = {w | w ∈ {x, y, z}∗}

; Machine starts in state 0.

; check for z
0x*r0
0y*r0
0z_rS
0 _ * * accept
0 * * r reject

; find symbol x,y and z, and shift them to left


S _ _ r Sempty
S y _ l Sy

17
BCS2113 Computer Science Theory
S x _ l Sx
S z _ l Sz
S * * r reject

; shift all blank to right


Sz _ z r S
Sy _ y r S
Sx _ x r S

; shift all x, y and z to left


Sempty _ _ l LMz
Sempty y _ l Sy
Sempty x _ l Sx
Sempty z _ l Sz
Sempty * * r reject

; at state Leftmost Z, shift x and y to left, and z to right


LMz _ _ l LMempty
LMz y y l LMz
LMz x x l LMz
LMz z _ r S

; when leftmost is empty, shift x and y to left, and delete z


LMempty _ _ r accept
LMempty z _ * S
LMempty y y l LMz
LMempty x x l LMz

; accept zone
accept _ _ r accept0
accept0 * * r accept0
accept0 _ _ r accept1
accept1 * : r accept2
accept2 * D r accept3
accept3 * _ r accept4
accept4 * B r accept5
accept5 * 0 r accept6
accept6 * 1 r accept7
accept7 * 1 r accept8
accept8 * 7 r accept9
accept9 * 0 r accept10
accept10 * 0 r accept11
accept11 * 0 r accept12
accept12 * 5 r accept13

18
BCS2113 Computer Science Theory
accept13 * _ r accept14
accept14 * B r accept15
accept15 * 0 r accept16
accept16 * 2 r accept17
accept17 * 1 r accept18
accept18 * 7 r accept19
accept19 * 0 r accept20
accept20 * 0 r accept21
accept21 * 2 r accept22
accept22 * 9 r accept23
accept23 * _ r accept24
accept24 * B r accept25
accept25 * 0 r accept26
accept26 * 1 r accept27
accept27 * 1 r accept28
accept28 * 7 r accept29
accept29 * 0 r accept30
accept30 * 0 r accept31
accept31 * 0 r accept32
accept32 * 9 r accept33
accept33 * _ * halt-accept

;reject zone
reject * * r reject
reject _ : r reject2
reject2 * ( r reject3
reject3 * _ r reject4
reject4 * B r reject5
reject5 * 0 r reject6
reject6 * 1 r reject7
reject7 * 1 r reject8
reject8 * 7 r reject9
reject9 * 0 r reject10
reject10 * 0 r reject11
reject11 * 0 r reject12
reject12 * 5 r reject13
reject13 * _ r reject14
reject14 * B r reject15
reject15 * 0 r reject16
reject16 * 1 r reject17
reject17 * 2 r reject18
reject18 * 7 r reject19
reject19 * 0 r reject20

19
BCS2113 Computer Science Theory
reject20 * 0 r reject21
reject21 * 2 r reject22
reject22 * 9 r reject23
reject23 * _ r reject24
reject24 * B r reject25
reject25 * 0 r reject26
reject26 * 1 r reject27
reject27 * 1 r reject28
reject28 * 7 r reject29
reject29 * 0 r reject30
reject30 * 0 r reject31
reject31 * 0 r reject32
reject32 * 9 r reject33
reject33 * _ * halt-reject

d) Link to the Turing Machine Simulator:


http://morphett.info/turing/turing.html?5d3e09c3f47bdef6f0933b8440d7bec2

Link to the slides:


https://docs.google.com/presentation/d/1x53BcFWNTH3I--nQyK-CcCDKAUZWEUmCZzFYrQD
Yjkw/edit?usp=sharing

Link to the video:


https://drive.google.com/file/d/1X6PQfd5jmUoyyC61JvCbFrXIzASGnVr6/view?usp=sharing

Slides used in the video

20
BCS2113 Computer Science Theory
21
BCS2113 Computer Science Theory
22
BCS2113 Computer Science Theory

Вам также может понравиться