52
Several games that children play fit the following description. Pieces are set up on a playing board. Dice are thrown (or a wheel is spun), and a number is generated at random. Depend
ing on the number, the pieces on the board must be rearranged in a fashion completely speci
fied by the rules. The child has no options about changing the board. Everything is deter
mined by the dice. Usual ly, it is then some other child's turn to throw the dice and make his or her move, but this hardly matters, because no skill or choice is involved. We could elimi
nate the opponent and have the one child move first the white pieces and then the black.
Whether or not the white pieces win the game is dependent entirely on what sequence of numbers is generated by the dice, not on who moves them.
Let us look at all possible positions of the pieces on the board and call them states. The game changes from one state to another in a fashion determined by the input of a certain number. For each possible number, there is one and only one resulting state. We should allow for the possibility that after a number is entered, the game is still in the same state as it was before. (For example, if a player who is in "jail" needs to roll doubles in order to get out, any other roll leaves the board in the same state.) After a certain number of rolls, the board arrives at a state that means a victory for one of the players and the game is over. We cal l this a final state. There might be many possible final states that result in victory for this player. In com
puter theory, these are also called halting states, terminal states, or accepting states.
Beginning with the initial state (which we presume to be unique), some input sequences of numbers lead to victory for the first child and some do not.
Let us put this game back on the shelf and take another example. A child has a simple computer (input device, processing unit, memory, output device) and wishes to calculate the sum of 3 plus 4. The child writes a program, which is a sequence of instructions that are fed into the machine one at a time. Each instruction is executed as soon as it is read, and then the next instruction is read. If all goes well, the machine outputs the number 7 and terminates execution. We can consider this process to be similar to the board-game. Here the board is the computer and the different arrangements of pieces on the board correspond to the differ
ent arrangements of O's and l 's in the cells of memory. Two machines are in the same state if their output pages look the same and their memories look the same cell by cell.
The computer is also deterministic, by which we mean that, on reading one particular input instruction, the machine converts itself from the state it was in to some particular other state (or remains in the same state if given a NO-OP), where the resultant state is completely
Yet Another Method for Defining Languages 53
detennined by the prior state and the input instruction. Nothing else. No choice is involved.
No knowledge is required of the state the machine was in six instructions ago. Some se
quences of input instructions may lead to success (printing the 7) and some may not. Success is entirely detennined by the sequence of inputs. Either the program will work or it will not.
As in the case of the board-game, in this model we have one initial state and the possi
bility of several successful final states. Printing the 7 is what is important; what is left in memory does not matter.
One small difference between these two situations is that in the child's game the number of pieces of input is detennined by whether either player has yet reached a final state, whereas with the computer the number of pieces of input is a matter of choice made before run time. Still, the input string is the sole determinant as to whether the game child or the computer child wins his or her victory.
In the first example, we can consider the set of all possible dice rolls to be the letters of an alphabet. We can then define a certain language as the set of strings of those letters that lead to success, that is, lead to a final victory state. Similarly, in the second example we can consider the set of all computer instructions as the letters of an alphabet. We can then define a language to be the set of all words over this alphabet that lead to success. This is the lan
guage whose words are all programs that print a 7.
The most general model, of which both of these examples are instances, is called a fi
nite automaton-"finite" because the number of possible states and number of letters in the alphabet are both finite, and "automaton" because the change of states is totally governed by the input. The detennination of what state is next is automatic (involuntary and mechanical), not willful, just as the motion of the hands of a clock is automatic, while the motion of the hands of a human is presumably the result of desire and thought. We present the precise defi
nition below. Automaton comes to us from the Greek, so its correct plural is automata.
DEFINITION
A finite automaton is a collection of three things:
1. A finite set of states, one of which is designated as the initial state, called the start state, and some (maybe none) of which are designated as final states.
2. An alphabet 2 of possible input letters.
3. A finite set of transitions that tel l for each state and for each letter of the input alphabet
which state to go to next. •
The definition above is incomplete in the sense that it describes what a finite automaton is but not how it works. It works by being presented with an input string of letters that it reads letter by letter starting at the leftmost letter. Beginning at the start state, the letters de
tennine a sequence of states. The sequence ends when the last input letter has been read.
Instead of writing out the whole phrase "finite automaton," it is customary to refer to one by its initials, FA. Computer theory is rife with acronyms, so we have many in this book.
The tenn FA is read by naming its letters, so we say "an FA" even though it stands for "a fi
nite automaton" and we say "two FAs" even though it stands for "two finite automata."
Some people prefer to call the object we have just defined a finite acceptor because its sole job is to accept certain input strings and reject others. It does not do anything l ike print output or play music. Even so, we shall stick to the tenninology "finite automaton." When we build some in Chapter 8 that do do something, we give them special names, such as "fi
nite automata with output."
Let us begin by considering in detail one particular example.
Suppose that the input alphabet has only the two letters a and b. Throughout this chap
ter, we use only this alphabet (except for a couple of problems at the end). Let us also as
sume that there are only three states, x, y, and z. Let the following be the rules of transition:
Let us also designate state x as the starting state and state z as the only final state.
We now have a perfectly defined finite automaton, because it fulfills all three require
ments demanded above: states, alphabet, transitions.
Let us examine what happens to various input strings when presented to this FA. Let us start with the string aaa. We begin, as always, in state x. The first letter of the string is an a, and it tells us to go to state y (by Rule I ). The next input (instruction) is also an a, and this tells us by Rule 3 to go back to state x. The third input is another a, and by Rule 1 again we go to state y. There are no more input letters in the input string, so our trip has ended. We did not finish up in the final state (state z), so we have an unsuccessful tennination of our run.
The string aaa is not in the language of all strings that leave this FA in state z. The set of all strings that do leave us in a final state is called the language defined by the finite au
tomaton. The input string aaa is not in the language defined by this FA. Using other tenni
nology, we may say that the string aaa is not accepted by this finite automaton because it does not lead to a final state. We use this expression often. We may also say, "aaa is rejected by this FA." The set of all strings accepted is the language associated with the FA. We say,
"this FA accepts the language L," or "L is the language accepted by this FA." When we wish to be anthropomorphic, we say that L is the language of the FA. If language L 1 is con
tained in language L2 and a certain FA accepts L2 (all the words in L2 are accepted and all the inputs accepted are words in L2), then this FA also must accept all the words in language L 1 (because they are also words in L2). However, we do not say, "L 1 is accepted by this FA" be
cause that would mean that all the words the FA accepts are in L 1 • This is solely a matter of standard usage.
At the moment, the only job an FA does is define the language it accepts, which is a fine reason for calling it an acceptor, or better still a language-recognizer. This last tenn is good because the FA merely recognizes whether the input string is in its language much the same way we might recognize when we hear someone speak Russian without necessarily under
standing what it means.
lowed the instruction of each input letter, we end up in state :. State ;;; is designated a final state, so we have won this game. The input string ahha has taken us successfully to the final state. The string ahba is therefore a word in the language associated with this FA. The word ahha is accepted by this FA.
It is not hard for us to predict which strings will be accepted by this FA. If an input string is made up of only the letter a repeated some number of times, then the action of the FA will be to jump back and forth between state x and state y. No such word can ever be
ac-Yet Another Method for Defining Languages 55
cepted. To get into state z, it is necessary for the string to have the letter h in it. As soon as a h is encountered in the input string, the FA jumps immediately to state z no matter what state it was in before. Once in state z, it is impossible to leave. When the input string runs out, the FA will still be in state z, leading to acceptance of the string.
The FA above will accept all strings that have the letter h in them and no other strings.
Therefore, the language associated with (or accepted by) this FA is the one defined by the regular expression
(a + b)*b(a + b)*
The list of transition rules can grow very long. It is much simpler to summarize them in a table format. Each row of the table is the name of one of the states in the FA, and each col
umn of the table is a letter of the input alphabet. The entries inside the table are the new states that the FA moves into -the transition states. The transition table for the FA we have described is table has all the information necessary to define an FA.
Instead of the lengthy description of the meaning of motion between states caused by input letters, FAs could simply and equivalently have been defined as static transition tables.
Any table of the form start state and the final states, will be an FA. Similarly, every three-state FA corresponds to such a table.
Even though it is no more than a table of symbols, we consider an FA to be a machine, that is, we understand that this FA has dynamic capabilities. It moves. It processes input.
Something goes from state to state as the input is read in and executed. We may imagine that the state we are in at any given time is lit up and the others are dark. An FA running on an input string then looks like a pinball machine in operation.
We may make the definition of FAs even more mathematically abstract (wi th no greater precision and decreased understanding) by replacing the transition table with a total function whose input is a pair of state and alphabet letter and whose output is a sin
gle state. This function is called the transition function, usually denoted & (lowercase Greek delta) (for reasons lost to computer historians). The abstract definition of an FA is then:
1. A finite set of states Q = I % q 1 q2 • • • } of which % is the start state.
2. A subset of Q called the fi nal states.
3. An alphabet I = {x1 x2 x3 • • •} .
4. A transition function
8
associating each pair of state and letter with a state:8(q;,X)
= xkWe shall never refer to this transition function again in this volume.
From the table format, it is hard to see the moving parts. There is a pictorial representa
tion of an FA that gives us more of a feel for the motion. We begin by representing each state by a small circle drawn on a sheet of paper. From each state, we draw arrows showing to which other states the different letters of the input alphabet will lead us. We label these ar
rows with the corresponding alphabet letters.
If a certain letter makes a state go back to itself, we indicate this by an arrow that re
turns to the same circle -this arrow is called a loop. We can indicate the start state by label
ing it with the word "start" or by a minus sign, and the final states by labeling them with the word "final" or plus signs. Notice that some states are neitht;r - nor + . The machine we have already defined by the transition list and the transition table can be depicted by the transition diagram
a
Sometimes, a start state is indicated by an arrow and a final state by drawing a box or an
other circle around its circle. The minus and plus signs, when employed, are drawn inside or outside the state circles. This machine can also be depicted as
a a
start
or
Every input string can be interpreted as traversing a path beginning at the start state and moving among the states (perhaps visiting the same state many times) and finally settling in some particular rest state. If it is a final state, then the path has ended in success. The letters of the input string dictate the directions of travel. They are the directions and the fuel needed for motion. When we are out of letters, we must stop.
Let us look at this machine again and at the paths generated by the input strings aaaabba and bbaabbbb.
Yet Another Method for Defining Languages
a
a
h h
0
57
When we depict an FA as circles and arrows, we say that we have drawn a directed graph. Graph theory is an exciting subject in its own right, but for our purposes there is no real need to understand directed graphs in any deeper sense than as a collection of circles and arrows. We borrow from graph theory the name directed edge, or simply edge, for the arrow between states. An edge comes from one state and leads to another (or the same, if it is a loop). Every state has as many outgoing edges as there are letters in the alphabet. It is possible for a state to have no incoming edges or to have many.
There are machines for which it is not necessary to give the states specific names. For example, the FA we have been dealing with so far can be represented simply as
a
Even though we do not have names for the states, we can still determine whether a par
ticular input string is accepted by this machine. We start at the minus sign and proceed along the indicated edges until we are out of input letters. If we are then at a plus sign, we accept the word; if not, we reject it as not being part of the language of the machine.
Let us consider some more simple examples of FAs.
EXAMPLE
a. h a
b
In the picture above, we have drawn one edge from the state on the right back into itself and given this loop the two labels a and b, separated by a comma, meaning that this is the path traveled if either letter is read. (We save ourselves from drawing a second loop edge.) We could have used the same convention to eliminate the need for two edges running from the minus state to the plus state. We could have replaced these with one edge with the label a, h, but we did not. At first glance, it looks as if this machine accepts everything. The first letter of the input takes us to the right-hand state and, once there, we are trapped forever. When the input string runs out, there we are in the correct final state. This description, however, omits the possibility that the input is the null string A. If the input string is the null string, we are left in the left-hand state, and we never get to the final state. There is a smal l problem about understanding how it is possible for A ever to be an input string to an FA, because a string, by definition, is executed (run) by reading its letters one at a time. By convention, we shall say that A starts in the start state and then ends right there on all FAs.
The language accepted by this machine is the set of all strings except A. This has the regular expression definitions
(a + b)(a + b)* = (a + b)+ •
EXAMPLE
One of the many FAs that accepts all words is
a , b
Here, the sign ::!::: means that the same state is both a start and a final state. Because there is only one state and no matter what happens we must stay there, the language for this machine is
(a + b)* •
Simi larly, there are FAs that accept no language. These are of two types: FAs that have no final states, such as
a , b a
b
and FAs in which the circles that represent the final states cannot be reached from the start state. This may be either because the picture is in two separate components as with
FAs and Their Languages 59
We consider these examples again in Chapter 1 1 .