The goal in the rest this chapter is to prove that regular languages, defined in Section 3.1, are precisely the languages accepted by finite automata. In order to do this, we will introduce a more general “device,” a nondeterministic finite automaton. The advantage of this approach is that it’s much easier to start with an arbitrary regular expression and draw a transition diagram for something that accepts the corresponding language and has an obvious connection to the regular expression. The only problem is that the something might not be a finite automaton, although it has a superficial resemblance to one, and we have to figure out how to interpret it in order to think of it as a physical device at all.
Example 3.6Accepting the Language
There is a close resemblance between the diagram in Figure 3.7 and the regular expression . The top loop corresponds to , the bottom one corresponds to , and the remaining -transition corresponds to the last in the regular expression. To the extent that we think of it as a transition diagram like that of an FA, its resemblance to the regular expression suggests that the string , for example, should be accepted, because it allows us to start at , take the top loop once, the bottom loop once, the top loop again, and finish up with the transition to the accepting state.
This diagram, however, is not the transition diagram for an FA, because there are three transitions from and fewer than two from several other states. The input string allows us to reach the accepting state, but it also allows us to follow, or at least start to follow, other paths that don’t result in acceptance. We can imagine an idealized “device” that would work by using the input symbols to follow paths shown in the diagram, making arbitrary choices at certain points. It has to be nondeterministic, in the sense that the path it follows is not determined by the input string. (If the first input symbol is , it chooses whether to start up the top path or down the bottom one.) Even if there were a way to build a physical device that acted like this, it wouldn’t accept this language in the same way that an FA could. Suppose we watched it process a string . If it ended up in the accepting state at the end, we could say that was in the language; if it didn’t, all we could say for sure is that the moves it chose to make did not lead to the accepting state.
Using nondeterminism to accept .
Allowing nondeterminism, by relaxing the rules for an FA, makes it easy to draw diagrams corresponding to regular expressions. However, we should no longer think of the diagram as representing an explicit algorithm for accepting the language, because an algorithm refers to a sequence of steps that are determined by the input and would be the same no matter how many times the algorithm was executed on the same input.
If the diagram doesn’t represent an explicit accepting algorithm, what good is it? One way to answer this is to think of the diagram as describing a number of different sequences of steps that might be followed. We can visualize these sequences for the input string by drawing a computation tree, pictured in Figure 3.8.
A level of the tree corresponds to the input (the prefix of the entire input string) read so far, and the states appearing on this level are those in which the device could be, depending on the choices it has made so far. Two paths in the tree, such as the one that starts by treating the initial as the first symbol of , terminate prematurely, because the next input symbol does not allow a move from the current state. One path, which corresponds to interpreting the input string as , allows all the input to be read and ends up in a nonaccepting state. The path in which the device makes the “correct” choice at each step ends up at the accepting state when all the input symbols have been read.
The computation tree for Figure 3.7 and the input string .
Transcription note
The printed tree labels its final nonaccepting leaf ; following Figure 3.7 gives . The live tree and simulator follow the transition diagram.
If we had the transition diagram in Figure 3.7 and were trying to use it to accept the language, we could systematically keep track of the current sequence of steps, and use a backtracking strategy whenever we couldn’t proceed any further or finished in a nonaccepting state. The result would, in effect, be to search the computation tree using a depth-first search. In the next section we will see how to develop an ordinary finite automaton that effectively executes a breadth-first search of the tree, by keeping track after each input symbol of all the possible states the various sequences of steps could have led us to.
Example 3.9Accepting the Language
In this example we consider the regular expression . The techniques of Example 3.6 don’t provide a simple way to draw a transition diagram related to the regular expression, but Figure 3.10 illustrates another type of nondeterminism that does.
The new feature is a “-transition,” which allows the device to change state with no input. If the input is received in state 0, there are three options: take the transition from state 0 corresponding to the in ; move to state 3 and take the transition corresponding to ; and move to state 3 and take the transition corresponding to the in . The diagram shows two -transitions from state 3, but because of the -transition, we would have a choice of moves even if there were only one.
Figure 3.11 shows a computation tree illustrating the possible sequences of moves for the input string . The -transition is drawn as a horizontal arrow, so that as in the previous example, a new level of the tree corresponds to a new input symbol.
Using nondeterminism to accept .
The computation tree for Figure 3.10 and the input string .
The string is accepted, because the device can choose to take the first loop, execute the -transition, and take the longer loop from state 3.
The transition diagrams in our first two examples show four of the five ingredients of an ordinary finite automaton. The one that must be handled differently is the transition function . For a state and an alphabet symbol , it is no longer correct to say that is a state: There may be no transitions from state on input , or one, or more than one. There may also be -transitions. We can incorporate both of these features by making two changes: first, enlarging the domain of to include ordered pairs as well as the pairs in ; and second, making the values of sets of states instead of individual states.
Definition 3.12A Nondeterministic Finite Automaton
A nondeterministic finite automaton (NFA) is a 5-tuple , where
- is a finite set of states;
- is a finite input alphabet;
- is the initial state;
- is the set of accepting states;
- is the transition function.
For every element of and every element of , we interpret as the set of states to which the FA can move, if it is in state and receives the input , or, if , the set of states other than to which the NFA can move from state without receiving any input symbol.
In Example 3.9, for example, , , , and .
Transcription note
The final expression is printed as . Figure 3.10 gives . The union below uses the printed ; its general form uses .
In the case of an NFA , we want to tell us all the states can get to by starting at and using the symbols in the string . We can still define the function recursively, but the mathematical notation required to express this precisely is a little more involved, particularly if has -transitions.
In order to define , where and , we start by considering , just as in the simple case of an ordinary FA. This is now a set of states, and for each state in this set, is itself a set. In order to include all the possibilities, we need to consider
Finally, we must keep in mind that in the case of -transitions, “using all the symbols in the string ” really means using all the symbols in and perhaps -transitions where they are possible. In the recursive step of the definition of , once we have the union we have just described, we must consider all the additional states we might be able to reach from elements of this union, using nothing but -transitions.
You can probably see at this point how the following definition will be helpful in our discussion.
Definition 3.13The -Closure of a Set of States
Suppose is an NFA, and is a set of states. The -closure of is the set that can be defined recursively as follows.
- .
- For every , .
In exactly the same way as in Example 1.21, we can convert the recursive definition of into an algorithm for evaluating it, as follows.
AlgorithmTo Calculate
Initialize to be . Make a sequence of passes, in each pass considering every and adding to every state in that is not already an element. Stop after the first pass in which is not changed. The final value of is .
A state is in if it is an element of or can be reached from an element of using one or more -transitions.
With the help of Definition 3.13 we can now define the extended transition function for a nondeterministic finite automaton.
Definition 3.14The Extended Transition Function for an NFA, and the Definition of Acceptance
Let be an NFA. We define the extended transition function
as follows:
- For every , .
- For every , every , and every ,
A string is accepted by if . The language accepted by is the set of all strings accepted by .
For the NFA in Example 3.9, which has only one -transition, it is easy to evaluate by looking at the computation tree in Figure 3.11. The two states on the first level of the diagram are 0 and 3, the elements of the -closure of . The states on the third level, for example, are 2, 3, and 4, because . When we apply the recursive part of the definition to evaluate , we first evaluate
and then we compute the -closure of this set, which contains the additional element 3.
For an NFA with no -transitions, both statements in the definition can be simplified, because for every subset of , .
We illustrate the definition once more in a slightly more extended example.
Example 3.15Applying the Definitions of and
We start by evaluating the -closure of the set in the NFA whose transition diagram is shown in Figure 3.16. When we apply the algorithm derived from Definition 3.13, after one pass is , after two passes it is , after three passes it is , and during the next pass it remains unchanged. The set is therefore .
Evaluating the extended transition function when there are -transitions.
Transcription note
The preceding paragraph prints after starting with . The calculated set is . The simulator follows Figure 3.16.
If we want to apply the definition of to evaluate , the easiest way is to begin with , the shortest prefix of , and work our way up one symbol at a time.
The evaluation of is very similar to that of , since there are no -transitions from , and the evaluation of is also similar. Because contains the accepting state , the string is accepted.
A state is an element of if in the transition diagram there is a path from to , in which there are transitions for every symbol in and the next transition at each step corresponds either to the next symbol in or to . In simple examples, including this one, you may feel that it’s easier to evaluate by looking at the diagram and determining by inspection what states you can get to. One reason for having a precise recursive definition of and a systematic algorithm for evaluating it is that otherwise it’s easy to overlook things.