Differences
This shows you the differences between two versions of the page.
Both sides previous revision Previous revision Next revision | Previous revision | ||
aa:lab:02 [2020/10/07 15:36] pdmatei |
aa:lab:02 [2020/10/22 10:25] (current) pdmatei |
||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== Lab 02 - Turing Machines ======= | + | ====== Lab 02 - Introduction to Turing Machines ======= |
**Key concepts** | **Key concepts** | ||
Line 6: | Line 6: | ||
- How is the execution of a TM defined? | - How is the execution of a TM defined? | ||
- | ==== Intro ==== | + | ==== 1. Intro ==== |
A **Turing Machine** consists of: | A **Turing Machine** consists of: | ||
* an **alphabet** $math[\Sigma] | * an **alphabet** $math[\Sigma] | ||
Line 20: | Line 20: | ||
* assembly instructions | * assembly instructions | ||
- | ==== A few basic Turing Machines ==== | + | ==== 1. A few basic Turing Machines ==== |
+ | **1.1** What does the following TM do? | ||
+ | $math[M=(K,\Sigma,q_0,\delta,F)] where $math[K=\{q_0,q_1,q_2\}], $math[F=\{q_2\}], $math[\Sigma=\{0,1,\#\}] and $math[\delta] is defined as below: | ||
- | - (Answers online) What does the following TM do? (**bitwise complement**) | + | ^ ^ 0 ^ 1 ^ # ^ |
- | - (Answers online) Write a TM which **accepts** only if the **input** is a binary encoding of a **even** natural number. | + | | $math[q_0] | $math[(q_0,1,R)] | $math[(q_0,0,R)] | $math[(q_1,0,L)] | |
- | - (Answers online) Write a TM which adds **5** to a number encoded in binary on the tape. The machine will always accept. | + | | $math[q_1] | $math[(q_1,0,L)] | $math[(q_1,1,L)] | $math[(q_2,\#,R)] | |
- | - (Answers online) Check if a symbol is present on the tape. | + | |
- | - (Discussion) How would the following algorithm be represented as a Turing Machine: | + | |
+ | **1.2** Write a TM which **enters the final state** only if the **input** is a binary encoding of an **even** natural number. | ||
+ | |||
+ | **1.3** Write a TM which verifies if a given word over alphabet $math[{A,B}] contains the sequence ''ABA''. | ||
+ | |||
+ | **1.4** Write a TM which adds **5** to a number encoded in binary on the tape. | ||
+ | |||
+ | **1.5** Write a TM which checks if a binary number ''x'' is strictly larger than ''y''. Hint: use a separator symbol between words. | ||
+ | |||
+ | ==== 2. Algorithms and Turing Machines ==== | ||
+ | |||
+ | How would the following algorithm be represented as a Turing Machine: | ||
<code> | <code> | ||
Algorithm(vector V, integer M) { | Algorithm(vector V, integer M) { | ||
Line 34: | Line 47: | ||
for-each x in V | for-each x in V | ||
s += x | s += x | ||
- | if (x > 1000) | + | if (s > M) |
then return 1 | then return 1 | ||
else return 0 | else return 0 | ||
Line 44: | Line 57: | ||
* how would ''foreach x in V'' be implemented? | * how would ''foreach x in V'' be implemented? | ||
* how would ''s += x'' be implemented? | * how would ''s += x'' be implemented? | ||
- | * how would ''if (x > 1000) then ... else ...'' be implemented ? | + | * how would ''if (s > M) then ... else ...'' be implemented ? |
+ | |||
+ | /* | ||
+ | |||
+ | **Answer:** | ||
+ | * The input of the tape should contain each element of the vector ''v'', encoded in binary, separated by a special character (e.g. ''@''). The last number in the sequence will be separated by another character (e.g. ''!'') from the value ''M''. | ||
+ | * The machine should accept if the algorithm returns 1, that is, if the sum of elements of the array is greater than ''M'' | ||
+ | * Before executing the foreach, the TM should //allocate// part of its tape for the sum ''s'' which is initially 0. The LHS of the tape could be used. | ||
+ | * The foreach can be easily implemented by moving the cursor between ''@'' characters. | ||
+ | * For a particular ''x'', we can implement binary adding between ''x'' and the current ''s'': | ||
+ | * M should go back-and-forth between the location of ''s'' and that of the current ''x''. | ||
+ | * As each bit of ''x'' is processed, it should be erased (writing ''#'') so that we can easily skip to the current ''x''. | ||
+ | * After ''!'' is read on the tape, we know we have finished the ''foreach''. We can then implement a bit-wise comparison of the values ''s'' and ''M'', which would now be the current value of the tape. The machine accepts if ''s > M''. | ||
- | Homework: | + | */ |
+ | ==== More practice exercises ==== | ||
* Write a TM which verifies if a string has the **same number** of ones and zeroes. Give hints - live (what should the machine do?) | * Write a TM which verifies if a string has the **same number** of ones and zeroes. Give hints - live (what should the machine do?) | ||
* write a TM which **accepts** a given regular expression | * write a TM which **accepts** a given regular expression | ||
* write a TM which **reverses** a given binary string (always accepts) | * write a TM which **reverses** a given binary string (always accepts) | ||