# CS 340 - Lecture 2

## Finite Automata

Last time we discussed regular expressions as generators for the strings that comprise a regular language.

Today we will discuss finite automata, which are abstract machines that serve as "recognizers" for the strings that comprise a regular language.  The idea is that we can feed an input string into a finite automaton, and it will answer "yes" or "no" depending on whether or not the input string belongs to the language that the automaton recognizes.

Note that not all possible languages are regular languages.  There are interesting classes of languages that cannot be generated by regular expressions or recognized by finite automata.

### Basics

Note that a correct finite automaton for language L must answer "yes" for every string that belongs to L and answer "no" to every string that does not belong to L.

A finite automaton consists of states and transitions.  We denote states as circles and transitions as arrows connecting one state to another.

Exactly one state of a finite automaton is designated as the start state.  The start state is often represented by marking it with a minus ("-") symbol.  JFLAP denotes the start state by marking it with a triangle.

At least one state of a finite automaton is designated as an accepting state.  It is legal for a finite automaton to have multiple accepting states.  An accepting state is often represented by marking it with a plus ("+") symbol.  JFLAP denotes an accepting state by drawing it with two circles, one inside each other.

To summarize:

Note that it is legal for the start state to also be an accepting state.

A transition from one state to another is labeled with a symbol (from the alphabet of the language recognized by the finite automaton) or with the special symbol epsilon ("ε").

### Algorithm

A finite automaton processes an input string according to the following algorithm.

The input string is read from a tape.  The tape head is initially positioned over the first symbol of the input string.

The current state starts out as the start state.

Loop {

(1) If the current state has a transition labeled ε:

Nondeterministic choice: either

- Choose a transition labeled ε to the next state (updating current state) and go back to beginning of loop

- Do nothing (go to step 2)

(2) if there are no more symbols to be read from the tape:

If the current state is an accepting state, then the input string is accepted and the algorithm terminates

If the current state is not an accepting state, then the input string is rejected and the algorithm terminates

(3) If the current state has no transition labeled with a symbol matching the symbol under the tape head:

The input string is rejected and the algorithm terminates

(4) Nondeterministic choice:

Choose a transition labeled with a symbol matching the one under the tape head.  Move the tape head forward one position.  Follow the transition to the next state (updating current state).

}

Example: for the following finite automaton:

The input string

aabababb

causes the algorithm to traverse the states

0 1 2 3 2 3 2 3 4

Because the input tape is empty when the algorithm reaches state 4 (an accepting state), the string is accepted.

### Nondeterminism

In the algorithm, two points were labeled as "nondeterministic choices".

Nondeterminism means that when the algorithm is faced with multiple choices for what to do next, it does them all "at the same time".  One way to think about this is that a parallel universe is created and the algorithm continues in both the current universe and the parallel universe, making a different choice in each universe.

Nondeterminism affects finite automata as follows: an automaton accepts an input string as part of the language recognized by the automaton as long as there is at least one universe in which the automaton accepts the input string.  The input string is rejected (not part of the language) if there is no universe in which the string is accepted.

### Deterministic vs. Nondeterministic Finite Automata

Only some finite automata can exhibit nondeterminism.  A finite automaton that never exhibits nondeterminism is called a deterministic finite automaton, or DFA.  A finite automaton that can exhibit nondeterminism is called a nondeterministic finite automaton, or NFA.

There are two features of finite automata that can lead to nondeterminism:

transitions labeled ε

multiple transitions out of a single state having the same label

Therefore, a DFA is simply a finite automaton that does not have either of these features.

Interestingly, DFAs and NFAs have the same amount of expressive power: given any NFA, we can construct a DFA that recognizes precisely the same language as the NFA, and vice versa.  However, to construct an equivalent DFA for an NFA may require an exponential increase in the number of states.

### JFLAP

JFLAP is a software package for constructing and simulating various kinds of automata, including finite automata.

Demo: using JFLAP to construct the DFA mentioned earlier.  Simulate on input strings:

aabb (accepts)

aababb (accepts)

aabba (rejects)

Lab 2