Week 2-2
Week 2-2
Chapter 12 textbook
Prepared by LC Kwek
Edited by NHAA Nov 2019
Objectives
2/20
Limitations of FSM (1/3)
• Not all things that can be computed could be computed with an FSA
• FSA below can check for strings a(a|b)*b
a, b
0 1 2
a b
• However, there is no way to build an FSA to check if there are
precisely the same number of a’s and b’s
• Let’s try to build a FSA!
3/20
Limitations of FSM (2/3)
a a
1a 0 1b
b b
4/20
Limitations of FSM (3/3)
a a a a
2a 1a 0 1b 2b
b b b b
a a a a a a
... 3a 2a 1a 0 1b 2b 3b ...
b b b b b b
5/20
The Turing Machine (1/8)
tape
read/write
FSM head
FSM
controller
controller
6/20
The Turing Machine (2/8)
• Tape
• Infinite length
• Divided into squares where each square is blank or contains one symbol
from a finite alphabet of symbols
• Records input and output
• Serves as memory
• Read/Write head
• Reads: symbols from the tape, one square at a time, usually beginning at the
left-most square that contains a symbol
• Writes: symbol to the tape
• Moves: one square to the right or left or stops
7/20
The Turing Machine (3/8)
8/20
The Turing Machine (4/8)
• Example:
• The controller starts out in state 0 at the left-most non-blank square
A B A A
9/20
The Turing Machine (5/8)
• Example: (cont.)
• If it reads ‘A’, ‘A’ remains unchanged, it moves right, and stays in state 0
A B A A
don’t write
move right
10/20
The Turing Machine (6/8)
• Example: (cont.)
• If it reads ‘B’, it will write an ‘A’, and not move, and change to state 1
ABAA
write ‘A’
don’t move
11/20
The Turing Machine (7/8)
• Example: (cont.)
• It halts as state 1 is an accepting state
AAAA
STO P
12/20
The Turing Machine (8/8)
13/20
Representations of TM (1/2)
• State diagram
• As what we have seen earlier …
• Pseudo-code
• The same TM can be represented as textual program code
14/20
Representations of TM (2/2)
• Table
• It can be also represented as a table
State Input Write Move New state
0 A A > 0
0 B A Stay 1
1 Any Nothing Stay Halt
• Set of tuples
• The items in a tuple are in the same order as the table
(0,A,A,>,0)
(0,B,A,-,1)
15/20
TM in Operation: Source Code
1 #include <stdio.h>
2 #define N 256 Enter string: ABAA
3 void changeBtoAthenStop(char *b); AAAA
4 int main(void){
5 char b[N];
6
7 printf("Enter string: ");
8 fgets(b,N,stdin); /* gets(b); */
9 changeBtoAthenStop(b);
10 fputs(b,stdout);
11 return(0);
12 }
13 void changeBtoAthenStop(char *b){
14 int k=0;
15
16 while(b[k]=='A') k++;
17 b[k]='A';
18 return;
19 }
16/20
Example 1: Bit Inverter
• A bit inverter TM
• Beginning in state 1 on the leftmost non-blank cell, it inverts whatever the
current symbol is by printing its opposite
• It keeps moving right while remaining in state 1
(1,0,>) (0,1,>)
(1,0,1,>,1)
1 (1,1,0,>,1)
0 1 1
1 1 1
1 0 1
1 0 0
17/20
Example 2: Binary Number Incrementor
(1/2)
19/20
Summary
20/20