# Finite Automata¶

Welcome to Week 2!

This week we will learn about our first model of computation: **Finite Automata** (Deterministic and Non-deterministic).

Learning Objectives for this week

- Encode
*problems*as sets of**strings**over an**alphabet**. - Use the concept of a
**language**(set of strings) to talk about decision problems. - Use a
**Finite Automaton**model of computation to decide if a string belongs to a language or not. - Specify and simulate
**Deterministic Finite Automata (DFAs)**. - Specify and simulate
**Non-deterministic Finite Automata (NFAs)**. - Describe the idea of
**non-determinism**and how it differs from**determinism**. - Abstractly specify Finite Automata (FAs) using mathematical notation.

Ask & Answer Questions

Don't forget that you can use the Aula's Common Room to raise questions for me and the rest of the class.

Add your questions to the Community Common Room and take a look at others' questions. If you see a question which you'd like to answer then just go ahead and participate in the discussion!

## Pre-class Activity¶

Here are two informal games to help you design simple machines, which we will employ as our first *model of computation*.

Even numbers game

Imagine you are playing a game where you are shown a number digit-by-digit left-to-right. Your task is to decide if the number is even.

- How do you
**decide**? - How much of the number do you need to
*remember*? (Memory) - How does your mental
**state**change after seeing each digit?

A number is even if its units digit is one of: 0, 2, 4, 6, 8.

We wait until we get the last digit then check if it is one of 0, 2, 4, 6, 8. If it is then we announce that the number is even, otherwise we announce that it is not (odd number).

We only need to *remember/store* **one** digit.

In terms of *state*, we would mostly be in a "wait for the last digit" state until we get the last digit and then we switch to a "we have seen the last digit, so we can make a decision now" state.

Multiples of 3 game

Now, imagine you are playing a game where you are shown a number digit-by-digit left-to-right, but your task this time is to decide if the number is divisible by 3.

- How do you
**decide**? - What do you need to
*remember*? (Memory) - How does your mental
**state**change after seeing each digit?

A number is divisible by 3 if its digits' sum is itself divisible by 3.

For example, 27 is divisible by 3 because 2+7=9 is divisible by 3.

29 is not because 2+9=11 is not divisible by 3.

In fact, we can use the trick above *recursively*: When we get 2+9=11, we sum the digits of 11 to get 1+1=2 which is easier to see as not being a multiple of 3.

We need to keep a running sum of the digits seen so far. We update this sum each time we are given a new digit. If this sum grows to more than one digit then we sum its digits again until it reduces to one digit (to keep things simple).

Once we are given the last digit, we update "the sum" and check if it is one of: 0, 3, 6, 9. If it is then we conclude that the number is a multiple of 3, otherwise it is not.

This time we need to *remember* this "sum" rather than the last digit.

Regarding *state*, you may think that 2 states would suffice like in the previous example, which is true provided that you have enough memory for the "sum".

A simpler approach that does not require "external memory" is to have enough states to hold the value of this sum. Here, the sum can take any of the ten values 0,...,9 so we can use 10 states, each representing a specific value of the sum.

For example, if the number is 123654 then the processing proceeds as follows:

Digit | Calculations | State |
---|---|---|

1 | 1 | |

2 | 1+2=3 | 3 |

3 | 3+3=6 | 6 |

6 | 6+6=12 then 1+2=3 | 3 |

5 | 3+5=8 | 8 |

4 | 8+4=12 then 1+2=3 | 3 |

The last state is 3, which is a multiple of 3, so we conclude that 123654 is divisible by 3. Indeed, we have 123654/3 = 41218. (Or written alternatively, 123654 = 3 * 41218.)

## Lecture videos¶

The PDF slides are available for download.

You are encouraged to download these. You can follow the videos without downloading them though.

Our prime aim for the first third of the module is to establish a reliable model of **computation**. We will start with the idea that a "computer" is in a **state** and as it processes a bit of information it moves from that state to another.

Let us formalise this intuition a bit.

### Languages¶

We will start with how we represent "bits of information" -- the concept of **languages**.

### Finite Automaton¶

Now we are ready to see our first model: **Finite Automaton**.

Let us delve a bit deeper. Let us see how we can **design** deterministic finite automata for various languages (= sets of strings).

And finally, mathematical notation will help us formalise the concept we have developed.
Think of it as a recipe to help you implement the machine in your favourite OOP language, i.e. what are the class *attributes* and *methods*.

### Non-determinism¶

Next, we will relax the rigidity of DFAs by allowing an arbitrary number of transitions from each state. This concept is called **non-determinism**.

Ready for the lab exercises?

Now, move to the lab section and try the problems.

We will meet in your timetabled lab session to help you with any challenging exercises.