0% found this document useful (0 votes)
26 views

2024 AAATut 1 With Solutions

...

Uploaded by

Michael Junior
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
26 views

2024 AAATut 1 With Solutions

...

Uploaded by

Michael Junior
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

U NIVERSITY OF THE W ITWATERSRAND , J OHANNESBURG

School of Computer Science and Applied Mathematics

Advanced Analysis of Algorithms – COMS3005A


Tutorial 1

Ian Sanders

Second semester, 2024 Academic Year

1 Aim
A brief refresher on Finite Automata, Pushdown Automata and Turing Machines. In this tutorial you will
be expected to trace the execution of given automata and to design automata to accept/recognise various
languages over their given alphabets.

2 Questions
1. Trace the FA given in Figure 1 on words from the alphabet {a, b} and try to determine what it
does.
a

a
S+− X+
a
b b

Y+ Z a,b

Figure 1: The finite automaton for question 1

Solution
The FA accepts the empty string, a+
(i.e. strings of one of more as), b+ i.e. strings of one of more
+ +
bs) and b a (one of more bs followed by one or more as. It does not accept any word where a b
follows an a.
So we could say that this is an FA accepting all words that do not contain the ab-substring.
End of solution

1
2. Trace the FA given in Figure 2 on words from the alphabet {a, b} and try to determine what it
does.

a b
b
A C+
a a

S−
b
a
B D+
b
b a

Figure 2: The finite automaton for question 2

Solution
Any word accepted by the top branch starts with an a and ends with a b. Any word accepted by
the bottom branch starts with an b and ends with a a.
The FA accepts words with different first and last letters.
End of solution

3. Design a deterministic finite automaton (DFA) that will recognise all of the words in the language
L over the alphabet {a, b}. Here L is the language that contains at least two ab substrings and
ends on a b. Note that if the word ends in the substring ab, then at least two other occurrences of
substring ab must precede this substring.
For example, aaaaabaaaabbbbbb and ababab are words in L but aaaaabaaaaab and abab are not.
Solution

b a b a a b
b
a b a b
S− A B C D E+
a

End of solution

2
4. Trace the execution of the PDA in Figure 4 to show that it accepts/recognises words in L =
{(ab)n (ba)n−2 | n > 2}.
Note that we do not draw in arcs that would go to a reject state as they would make the diagram
too cluttered. For a determinsitic PDA we assume that these arcs do, however, exist. For example,
if the PDA was in state Read 3 and read a b then there should be an arc to a reject state as this
would imply a word that is not in the language.

Start

b a X
Read 1 Read 3 Pop 1 Pop 2

X
Push X a

X
Read 2 Read 4 Pop 3

∆ b a


Accept Pop 4 Read 5

Figure 3: The pushdown automaton for question 4

Solution
The PDA works by pushing an X onto the stack for each ab read (Read 1 and Read 2). When Read
2 reads a b then an a must be read next. If this is a case then an X is popped from the stack. Once
all of the ba have been read then two Xs must still be popped before popping a ∆ to ensure that
the stack is empty.
The smallest word here is when n = 3 i.e. (ab)3 (ba). Trace the PDA with this word and longer
words.
End of solution

3
5. Design a deterministic pushdown automata (DPDA) that accepts the language
L = {(a)n (b)n+1 (bb) | n ≥ 0} over the alphabet Σ = {a, b}.
Solution
Smallest word is bbb or (b)1 (bb)
For the shortest word a b is read and the stack is found to be empty. Then two more bs are read
before checking the stack and the input are empty. Shortest word is accepted.
For a general word, the first loop pushes an X for every a read. When a b is read then an X is
popped. When a b is read and the stack is empty then the DPDA has just read the extra b. Then (as
for the shortest word case) two more bs are read before checking the stack and the input are empty.

b ∆ b
Start Read Pop Read Read

X b
a b

Push X Read Pop

Read

Accept

End of solution

4
6. Trace the execution of the TM in Figure 6 to show that it
• loops on all words that begin with 0,
• changes all other instances of 0 on the input tape into 1.
In this case the input alphabet must obviously be Σ = {0, 1}.

(0, 1, R),(1, 1, R)

(1, 1, R) (∆, ∆, L)
start q1 halt

(0, 0, R)

qs

(0, 0, R),(1, 1, R),(∆, ∆, R)

Figure 4: Changing 0s to 1s

Solution
Any word starting with a 0 is not accepted – the TM will loop forever.
If a word starts with 1, a 1 is written and the head moves right and the TM moves to state q1.
In state q1 every time a 1 is read, a 1 is written and the head moves right. In state q1 every time a
0 is read, a 1 is written and the head moves right.
In both cases the TM stays in state q1.
If in state q1 a ∆ is read then a ∆ is written and the head moves left. The state changes to Halt.
This TM changes any 0 in a binary number, that begins with a 1, to a 1. For example 1011 becomes
1111.
End of solution

5
7. The TM in Figure 5 determines whether a given word contains at least one instance of the substring
bab. If it does then the TM writes a T on the tape after the input word.
Trace the TM on different inputs to show it performs as expected.

(a,a,R)
(b,b,R) (b,b,R)

(b,b,R) (a,a,R) (b,b,R) (∆,T ,R)


Start 1 2 3 Halt

(a,a,R) (b,b,R)
(a,a,R)

(a,a,R)

Figure 5: TM for question 7

Solution
Try a word like aababaaab
The first a takes the TM to state 4. Note each letter is read off the tape and the same letter is written
back.
The second a take it to state 4.
The b in position 3 takes it to state 1
The a takes it to state 2.
The b take it to state 3.
In state 3 bab has already been found.
The remaining letters are just read off and written back until a ∆ is found. In this case a T is
written to the tape immediately after the input word and the TM halts.
Try to trace the TM with other inputs.
End of solution

6
8. Design a Turing Machine (TM) that

• accepts all words in {(b)n+2 an | n ≥ 1},


• loops forever on all words starting with a, and
• rejects all other words.

Assume that the alphabet is Σ = {a, b}.


Hint:
Write out your solution as high level pseudocode before you start drawing your TM. This will help
you to formulate an approach to solving the problem.
Solution
From start on b go to main part and on a go to a state where there is an infinite loop (keeps on
reading and writing the same things).
In main part: if read an b mark it off by X then go to end of input and moving left look for a Mark
this as a Y
Then go back until find an X then go right to look for an b again. Repeat.
When we find a b while moving left to find an a check if there is exactly one such b (the other extra
b was already converted to an X on the forward pass.)
If this is the case then go to halt state.

(a, a, R),
(b, b, R),
(Y, Y, R) (Y, Y, L)

(b, X, R) (∆, ∆, L)
Start 1 2

(b, B, L)
(a, a, R) (b, X, R) (a, Y, L)

(X, X, R)
7 4 3 6 Halt
(X, X, R)

(a, a, R), (a, a, L),


(b, b, R), (b, b, L)
(∆, ∆, R)

End of solution

You might also like