## Automata Homeworks for the course CS 3350, Spring 2020

• Unless explicitly specified, each homework should be turned in on a sheet of paper:
• if requested, draw an automaton like we did in class and like the book does,
• do whatever else is requested,
• place your homework on the instructor's desk in the classroom by the beginning of the class on the day when the homework is due.
• The main purpose of most homeworks is to show how well you understand the algorithms. In many cases, the resulting finite automata, pushdown automata, and Turing machines can be simplified, but please first literally apply the algorithm so that we know that you can use it. If in addition to this, you also show how to make the corresponding Turing machine or finite automaton or whatever more concise, nothing wrong with that, the TA may even give you some extra points (if she has time to grade these additional things). But the most important thing is to show that you can follow the algorithm. For simple examples that we give you as homeworks, you may immediately see how to convert, e.g., a context-free grammar into a Chomsky normal form, but if someone gives you a more complex case, you will have to use the algorithm. So, it is important to learn how to follow the algorithm. If you deviate from the algorithm, how do we know that you learned the algorithms? It was the same with sorting. Of course, if someone gives you a list of 4 numbers on the test, you can sort them yourself easily, the purpose of the question was that you show that you understand mergesort, quicksort etc., not that you sort 4 numbers. If after you show that you understand the algorithm you also provide a simpler answer -- great, but not instead of following the algorithm.

1. (Due January 22) In class, we studied an automaton for recognizing signed or unsigned binary integers. This automaton is easy to modify so that it will recognize all integers. The resulting automaton has 4 states: start (st), sign (si), integer (i), and error (e). Start is the starting state, integer is the only final state. The transitions are as follows:

• from st, any digit 0, 1, ..., 9 leads to i; + or − lead to si, every other symbol leads to e;
• from si, 0, 1, ..., or 9 lead to i, every other symbol leads to e;
• from i, 0, 1, ..., or 9 lead to i, every other symbol leads to e;
• from e, every symbol leads to e.
Trace, step-by-step, how this finite automaton will check whether the following two words (sequences of symbols) represent syntactically correct integers:
• the word −20 (which this automaton should accept) and
• the word +2−0 (which this automaton should reject).
Write down the tuple <Q, Σ, δ, q0, F> corresponding to this automaton:
• Q is the set of all the states,
• Σ is the alphabet, i.e., the set of all the symbols that this automaton can encounter; for simplicity, consider only five symbols: +, −, 0, 1, and letter a;
• δ: Q x Σ → Q is the function that describes, for each state q and for each symbol s, the state δ(q, s) to which the automaton that was originally in the state q moves when it sees the symbol s (you do not need to describe all possible transitions this way, just describe two of them);
• q0 is the staring state, and
• F is the set of all final states.
Apply the general algorithm for union to automata recognizing signed and unsigned integers.

2. (Due January 30)

• Use the general algorithm that we learned in class to design a non-deterministic finite automaton that recognizes the language l(a U n)*; reminder: AB means concatenation;
• transform the resulting non-deterministic finite automaton into a deterministic one.

3. (Due February 6) Write and test a method that emulates a general finite automaton. The input to this method should include:

• the number N of states q0, ..., qN − 1; we assume that q0 is the start state;
• the number M of symbols s0, ... sM − 1;
• an integer array state[n][m] that describes to what state the finite automaton moves if it was in the state qn and sees the symbol sm;
• a boolean array final[n] that tells us, for each state, whether this state is final or not.
This program needs to keep track of a current state. Initially, this location is 0.

Your program should emulate the work of the finite automaton step-by-step. Return the printout of the method, the printout of the program that you used to test this method, and the printout of the result of this testing. Feel free to use Java, C, C++, Fortran, or any programming language in which the code is understandable.

4. (Due February 6) Apply the general algorithm for transforming the finite automaton into a regular language (i.e., a language described by a regular expression) to the following automaton. This automaton has two states: a and b, a is the starting state and both a and b are final states. The only two symbols are 0 and 1. From a, 0 leads to a, and 1 to b. From b, both 0 and 1 lead to a.

5. (Due February 13) Prove that the following language is not regular {a2nbn} = {Λ, aab, aaaabb, aaaaaabbb, ...}. Together with your proof, also provide the detailed proof that we had in class: that the language {0n1n, n = 0, 1, 2, ...} = {Λ, 01, 0011, ...} is not regular.

6. (Due February 13) Show, step by step, how the following pushdown automaton will recognize a sequence 000111. This pushdown automaton has two states:

• the starting state s, and
• the final state f.
The transitions are as follows:
• From s to s, the transition is 0, ε, → 0;
• From s to f, the transition is: ε, ε → ε;
• From f to f, the transition is 1, 0 → ε.

7. (Due February 13) Show, step by step, how the grammar with rules S → ε and S → 0S1 will generate the word 000111.

8. (Due February 20) On the example of the automaton from Homework 4, show how the general algorithm will produce a context-free grammar that generates all the words accepted by this automaton -- and only words generated by this automaton. On the example of a word 101 accepted by this automaton, show how the tracing of acceptance of this word by the finite automaton can be translated into a generation of this same word by your context-free grammar.

9. (Due February 20) Use a general algorithm to construct a (non-deterministic) pushdown automaton that corresponds to context-free grammar with the starting variable S and the following rules:

• S → abA;
• A → baS; and
• S → ε.
Show, step by step, how the word abba will be accepted by this automaton. Its derivation in the given grammar is straightforward: S → abA → abbaS → abba.

10. (Due February 27) Transform the grammar from Homework 9 into Chomsky normal form.

11. (Due March 5) Use the general algorithm to transform the following pushdown automaton into a context-free grammar. This automaton recognizes words of the type qa...aca...aq, where the number of a's in both halves of the word is the same. This automaton has 4 states:

• the starting state s,
• the beginning state b,
• the ending state e, and
• the final state f.
The transitions are as follows:
• From s to b, the transition is q, ε → q;
• From b to b, the transition is a, ε → a;
• From b to e, the transition is c, ε → ε
• From e to e, the transition is a, a → ε
• From e to f, the transition is: q, q → ε.
Show, step-by-step, how the resulting grammar will generate the sequence qacaq.

12. (Due March 12) Show, step by step, how the stack-based algorithm will transform the expression (1 − 2) * (7 + 2) into a postfix expression, and then how a second stack-based algorithm will compute the value of this postfix expression.

13. (Due March 12) Write a program that, given an arithmetic expression,

• first transforms it to a postfix form, and then
• computes its value (by using the stack-based algorithms that we recalled in class).
You can assume that all the numbers in the arithmetic expression are one-digit numbers, i.e., each of these numbers is either 0, or 1, or 2, ..., or 9. For example, your program should correctly process expressions like 2+3*4, but there is no need to process expressions like 11+22.

• as with all programming assignments for this class, submit a printout of the code, and a printout of an example of what this program generates on each step;
• ideally, use Java, but if you want to write it in some other programming language, check with the TA that it is OK; usually, C or C++ are OK.

14. (Due March 12) For the LL(1) grammar that we studied in class, with rules S → F; S → (S + F); and F → a;, show how the word ((a + a) + a) can be represented as uvxyz in accordance with the pumping lemma for context-free grammars. Show that the corresponding word uvvxyyz will be generated by this grammar.

15. (Due March 12) Prove that the language consisting of all the words that have equal number of a's and b's and exactly twice as many c's is not context-free.

16. (Due April 9) Design a Turing machine that, given a positive unary number n, add 2 to this number. Test it, step-by-step, on the example of n = 3.

17. (Due April 9) Design a Turing machine that, given a positive binary number n greater than or equal to 2, subtracts 2 from this number. Test it, step-by-step, on the example of n = 4.

18. (Due April 9) Use the general algorithm to transform a finite automaton from Homework 4 into a Turing machine. Show step-by-step, on an example of a word 101, how this word will be recognized by your Turing machine.

19. (Due April 16) Write a method that emulates a general Turing machine. The input to this method should include:

• the number N of states q0, ..., qN − 1; we assume that q0 is the start state, that the last-but-one state qN − 2 is the accept state, and the last state qN − 1 is the reject state;
• the number M of symbols s0, ... sM − 1; we assume that s0 is the blank state _;
• an integer array state[n][m] that describes to what state the head of the Turing machine changes if it was in the state qn and sees the symbol sm;
• an integer array symbol[n][m] that describes what symbol should be on the tape after the head in the state qn sees the symbol sm (it may be the same symbol as before, or it may be some other symbol written by the Turing machine);
• a character array lr[n][m] that describes, for each state qn and for each symbol sm, whether the head moves to the left (L), or to the right (R), or stays in place (blank symbol);
• the integer array of a large size describing the original contents of the tape, i.e., what symbols are written in each cell.
This program needs to keep track of a current location of the head. Initially, this location is 0.

Your program should emulate the work of the Turing machine step-by-step. Return the printout of the method, the printout of the program that you used to test this method, and the printout of the result of this testing. Feel free to use Java, C, C+++, Fortran, or any programming language in which the code is understandable.

20. (Due April 16) As we discussed in a lecture, a Turing machine can be described as a finite automata with two stacks:

• the right stack contains, on top, the symbol to which the head points; below is the next symbol to the right, then the next to next symbol to the right, etc.;
• the left stack contains, on top, the symbol directly to the left of the head (if there is a one), under it is the next symbol to the left, etc.
On the example a Turing machine that computes n + 1 for n = 2, show, step-by-step:
• how each state of the corresponding Turing machine can be represented in terms of two stacks, and
• how each transition from one state to another can be implemented by push and pop operations.

21. (Due April 16) Give two examples:

• an example of computation time which makes an algorithm feasible according to the formal definition but not practically feasible, and
• an example of computation time for which the corresponding algorithm is practically feasible, but not feasible according to the formal definition.
These examples should be different from what you learned in class -- a minor difference is OK.

22. (Due April 23) Prove that the square root of 3 is not a rational number.

23. (Due April 23) What is NP? What is P? What is NP-complete? What is NP-hard? Give brief definitions. Give an example of an NP-complete problem. Is P equal to NP?

24. (Due April 30) Reproduce the proof of the halting problem in all the necessary detail.