Tutorial 1: Modeling Stuff with Finite Automata (draft)

Man, goat, wolf and cabbage. The following riddle is attributed to Alcuin of York (735-804AD) an English scholar, poet, educator, monk, and a friend of the emperor Charlemagne. He is also credited with creating the modern alphabet.

A man came to a river. He had with him a wolf, a goat and a cabbage. The boat can hold only one passenger besides the man. He can not leave the wolf with the goat, or the goat with the cabbage. How can he get everything across safely? How many trips will it take?

To solve this riddle, we will model the process in terms of states and transitions. Suppose the man is now on the west side on the river and wants to cross to the east side. Each state shows who is on the east side and who is on the west side. For example, in (mg|wc), man and goat are on the west side, and wolf and cabbage are on the east side. The transitions correspond to trips across the river. We label the transitions to indicate who is on the boat. The complete diagram looks like this:

From this diagram, the following sequence of trips constitutes a solution:

(mwgc|) - (wc|mg) - (mwc|g) - (w|mgc) - (wmg|c) - (g|mwc) - (mg|wc) - (|mwgc)

Believe it or not, we will be playing with such diagrams for the next two weeks or so. We will use the term finite automaton to refer to such a diagram. Finite automata are just a bunch of states with some arcs connecting them. These arcs are called transitions and are labelled by actions. Other common terms are: finite-state automata, finite-state systems, finite-state machines, state diagrams, transition diagrams, etc. In this class, instead of "action", we will usually use the term symbol or letter for the transition labels. We will often distinguish one state as the initial state. Some states can be distinguished as final states. (As in the automaton above.) In some situations, one can associate outputs with transitions or states. Automata with outputs are often called transducers.

Vending machine. Here is an example of an automaton with outputs associated with transitions. Think of a vending machine. Each soda costs 25 cents. The machine accepts nickels, dimes and quarters. We will assume that the machine does not give change, but it keeps track of the credit. Whenever the machine collects 25 cents, it dispenses a can of soda.

So our automaton will have states corresponding to the current credit: 0, 5, 10, 15, 20, and transitions labelled by n (nickel), d (dime) and q (quarter). For example, from 0 we have a transition to 10 labelled by d. What is the transition from 15 on d? After receiving a dime, the credit is 25 cents, so we will dispense a soda and the credit becomes 0. Thus we get a transition from 15 to 0 labelled by 10/S. The "10" is the input symbol (action) and the "S" is the output symbol (or output action -- dispensing a soda) that corresponds to this transition. Here is the complete automaton.

Trigger. Suppose we have a bit channel over which we send commands. Each command is identified by a specific bit string. We want to design a trigger that will fire a missile (whatever) as soon as the bit sequence 0110 appears on the channel.

One possibility would be to design a little gadget that stores the last 4 bits in a buffer and at each step compares the buffer to 0110 to see if this is the same string. That migth work for 4 bits just fine, but if the command is longer then comparing the buffer to the command string would cause delay during which we could get nuked by the enemy, and that would be unpleasant and bad for our egos.

So here is what we do. We design a little finite automaton that will enter a designated state (in which the red button gets pushed) whenever the last four digits are 0110. The idea behind this automaton is quite clever. Each state corresponds to the longest prefix of 0110 that is also a suffix of the bit stream received so far. (Please read the previous statement aloud three times before proceeding.) We allow an empty string that we denote l. So there are four possible prefixes: l, 0, 01, 011, 0110. The trick is now that we can determine the transitions between them without considering the actual bit stream.

Here is how it works. Suppose you are in state 0. That means that 0 is a suffix of the bit stream but 01 is not. Suppose the next received bit is 1. What state do we go to? We know that 01 is now a suffix of the bit stream. Is it the longest prefix of 0110 that is a suffix of the stream? It must be, since otherwise at the earlier step 0 would not be the longest! So we conclude that from 0 on 1 we go to 01. Similarly, from e on 0 we go to 0, from 01 on 1 we go to 011, and from 011 on 0 we go to 0110.

What about other transitions? Say we are in 0 and we get 0. Clearly, 0 is now a suffix. Is a longer suffix possible? Again, it's not, for essentially the same reason as before. So we have a transition from 0 on 0 to 0. Etc, etc. Once we work all transitions out, we get the folloing automaton:

The example above is much more important than it may seem. Problems of this type appear quite frequently in various application. In string matching, we want to detect all occurrences of a pattern P in a text T. To solve it, we can build an automaton for P like the one above, and run it through T. It turns out that many fast string matching algorithms are indeed based on this idea. The string matching problems are everywhere: lexical analysis in compilers, word processing, data mining, etc etc. Often they require finding occurrences of many strings, but the above idea extends to many patterns as well. We will talk about it later this quarter in more detail.

Finite automata are also a fundamental tool for the analysis and synthesis of sequential circuits.

TCP Finite State machine. The last example is from computer networks:

More info can be found at TCP/IP-Reference Model