Ch 8. Automata and Grammars
8.2 Finite State Machines and Automata
This section introduces the concept of finite state machines, explores their relationship with formal languages, and explains how automata are used to recognize patterns and define computational behaviour. These mathematical models help computer systems, such as compilers, network protocols, and artificial intelligence, respond in predictable ways.
A finite state machine (FSM) is a mathematical model of computation that consists of:
- A finite set of states
- A finite set of input symbols
- A transition function that maps a state and an input symbol to a new state
- A start state
- A set of accepting states
FSMs are used to model systems that can be in one of a finite number of states and change states based on input.
Example 8.15
Consider a login system that accepts a username and a password. The FSM might have the following states: Start, UsernameEntered, PasswordEntered, Authenticated, Error.
The transitions depend on user input. For example:
- From
Start, entering a valid username moves toUsernameEntered - From
UsernameEntered, entering a valid password moves to Authenticated - Invalid input at any stage moves to
Error
This FSM models the logic of a login system and ensures that only valid input sequences result in authentication.
A transition diagram is a graphical representation of an FSM or automaton. It illustrates states as labelled circles, transitions as directed arrows with input symbols labelled on them, a start state, usually indicated by an arrow pointing to it from nowhere, and accepting states, often marked with a double circle.
Transition diagrams help visualize how a system moves from one state to another in response to input, making it easier to understand and design FSMs.
Example 8.16
The following transition diagram is for an FSM that accepts binary strings ending in 01.

For example, the string 11001001, enters at state [latex]q_{0}[/latex] and then transitions through states
[latex]q_{0}(1), q_{0}(1), q_{1}(0), q_{1}(0), q_{2}(1), q_{1}(0), q_{1}(0), q_{2}(1)[/latex]
and the string is accepted. On the other hand, the string 11001000 transitions through states
[latex]q_{0}(1), q_{0}(1), q_{1}(0), q_{1}(0), q_{2}(1), q_{1}(0), q_{1}(0), q_{1}(0)[/latex]
and is not accepted.
An input string is a finite sequence of symbols taken from a defined input alphabet. It is fed into an FSA or automaton, which processes the string symbol by symbol, transitioning between states according to its transition function.
Example 8.17
Suppose the alphabet is the finite set of symbols [latex]\{ 0, 1 \}[/latex] and the input string is a sequence like 0101, 111, or 000. The machine reads the input string from left to right and, based on the current state and the symbol read, moves to the next state.
An output string is a sequence of symbols produced by a finite-state transducer, a type of FSM that generates output as it processes input. The output can be associated with transitions or states. The output string may be used to encode, translate, or transform the input string.
In a transition diagram with input and output strings, we write “input”/”output” next to each directed arrow, where “input” and “output” correspond to the input and output of the state, respectively.
Example 8.18
Let’s define an FSA that takes a binary input string and outputs a string where each symbol indicates whether the number of 1s seen so far is even (E) or odd (O). The states are [latex]q_{\text{even}}[/latex] and [latex]q_{\text{odd}}[/latex]. The alphabet has input {0, 1} and output {E, O}. The start state is [latex]q_{\text{even}}[/latex]. The transitions are as follows:
- [latex]q_{\text{even}}[/latex] with input
0[latex]\rightarrow q_{\text{even}}[/latex] with outputE - [latex]q_{\text{even}}[/latex] with input
1[latex]\rightarrow q_{\text{odd}}[/latex] with outputO - [latex]q_{\text{odd}}[/latex] with input
0[latex]\rightarrow q_{\text{odd}}[/latex] with outputO - [latex]q_{\text{odd}}[/latex] with input
1[latex]\rightarrow q_{\text{even}}[/latex] with outputE
The transition diagram is shown below.

As an example, consider the input string 1010. The following processing occurs:
- Start at [latex]q_{\text{even}}[/latex]
- Read
1:move to [latex]q_{\text{odd}}[/latex], outputO - Read
0:stay in [latex]q_{\text{odd}}[/latex], outputO - Read
1:move to [latex]q_{\text{even}}[/latex], outputE - Read
0:stay in [latex]q_{\text{even}}[/latex], outputE
A finite-state automaton (FSA) is a mathematical model used to represent and analyze systems that can be in one of a finite number of states and transition between those states based on input symbols. FSAs are foundational in computer science, especially in the study of formal languages, compilers, and digital circuit design. There are two main types of FSAs as outlined below.
A deterministic finite automaton (DFA) is a type of FSA in which, for each state and input symbol, there is exactly one transition. The machine processes input strings symbol by symbol, and if it ends in an accepting state, the input is accepted; otherwise, it is rejected.
A DFA is formally defined as a 5-tuple: [latex](Q, \Sigma, \delta, q_{0}, F)[/latex], where [latex]Q[/latex] is a finite set of states, [latex]\Sigma[/latex] is the input alphabet, [latex]\delta[/latex] is a transition function [latex]Q \times \Sigma \rightarrow Q[/latex], [latex]q_{0}[/latex] is the start state, and [latex]F[/latex] is a set of accepting states.
Example 8.19
Let’s define a DFA that accepts binary strings with an even number of 0s.
The states are [latex]q_{\text{even}}[/latex] and [latex]q_{\text{odd}}[/latex]. The alphabet is [latex]\Sigma[/latex] = {0, 1}.
The transitions are [latex]\delta (q_{\text{even}},[/latex]0[latex]) = q_{\text{odd}}[/latex], [latex]\delta (q_{\text{even}},[/latex]1[latex]) = q_{\text{even}}[/latex], [latex]\delta (q_{\text{odd}},[/latex]0[latex]) = q_{\text{even}}[/latex], and [latex]\delta (q_{\text{odd}},[/latex]1[latex]) = q_{\text{odd}}[/latex].
The start state is [latex]q_{\text{even}}[/latex]. The accepting state is [latex]q_{\text{even}}[/latex].
The transition diagram is shown below.

This DFA transitions between states whenever a 0 is read and remains in the same state when a 1 is read.
A string is accepted by an FSA if, after processing all its symbols, the automaton ends in an accepting state.
Example 8.20
Consider the DFA in Example 8.16 that accepts strings ending in 01. The input string 1101 is processed as follows:
- Start at [latex]q_{0}[/latex]
- Read
1:stay at [latex]q_{0}[/latex] - Read
1:stay at [latex]q_{0}[/latex] - Read
0:move to [latex]q_{1}[/latex] - Read
1:move to [latex]q_{2}[/latex]
Since the automaton ends in [latex]q_{2}[/latex], which is an accepting state, the string 1101 is accepted.
An automaton accepts a string if the string is processed completely and the automaton ends in an accepting state. This is the inverse phrasing of “accepted by.” This terminology is often used when describing the behaviour of the automaton itself.
Example 8.21
Using the same DFA as Example 8.16, the automaton accepts the string 1101 because it ends in the accepting state [latex]q_{2}[/latex]. It does not accept the string 1110 because it ends in [latex]q_{1}[/latex], which is not an accepting state. We describe the automaton's behaviour as accepting all strings that end in 01.
In automata theory, representing refers to how a language, string, or system is modelled using an FSA. It describes the use of states, transitions, and symbols to represent the behaviour or structure of a language.
Example 8.22
Suppose we want to represent the language of all binary strings that contain an even number of 0s. We could use the DFA in Example 8.19. The structure of the automaton models the rules of the language.
A non-deterministic finite automaton (NFA) allows multiple transitions on the same input symbol and transitions that do not consume input (called [latex]\epsilon[/latex]-transitions). NFAs are more flexible and easier to design than DFAs, but they are equivalent in power: every NFA has a corresponding DFA that accepts the same language.
Example 8.23
The states are [latex]q_{0}[/latex], [latex]q_{1}[/latex], and [latex]q_{2}[/latex]. The alphabet is [latex]\Sigma[/latex] = {0, 1}.
The transitions are [latex]\delta (q_{0},[/latex] 0[latex]) = \{ q_{0}, q_{1} \}[/latex], [latex]\delta (q_{0},[/latex] 1[latex]) = \{ q_{0} \}[/latex], and [latex]\delta (q_{1},[/latex] 1[latex]) = \{ q_{2} \}[/latex].
The start state is [latex]q_{0}[/latex]. The accepting state is [latex]q_{2}[/latex].
This NFA accepts any string that ends in 01.
In automata theory, two automata (such as DFAs or NFAs) are said to be equivalent if they accept the same language, that is, they accept the same set of input strings. This means that no matter how differently the automata are structured (e.g., number of states or transitions), they behave identically in terms of which strings they accept and which they reject.
Formally, let [latex]A_{1}[/latex] and [latex]A_{2}[/latex] be two finite automata. They are equivalent if [latex]L(A_{1}) = L(A_{2})[/latex], where [latex]L(A)[/latex] denotes the language accepted by automaton [latex]A[/latex].
Example 8.24
Define a language [latex]N[/latex] consisting of all binary strings that end in 01 as in Example 8.23. This NFA accepts strings ending in 01 by nondeterministically guessing the start of the 01 suffix.
Define a language [latex]D[/latex] that accepts binary strings ending in 01 as in Example 8.16. This DFA deterministically tracks the last two symbols to check if they form 01.
Even though the NFA uses nondeterminism and the DFA uses deterministic transitions, both automata accept the same set of strings, those ending in 01. Therefore, they are equivalent.
Real-World Example 8.2: Smart Card Access System
Suppose you and other employees use a smart card access system at a secure office building, where you swipe your cards at a reader to gain entry. The access system that controls the reader must validate the card, check access permissions, and unlock the door if the employee is authorized. It can be modelled as an FSM with the states Idle, CardRead, Validating, AccessGranted, and AccessDenied. The Idle state waits for a card swipe; the CardRead state occurs when the card has been swiped; the Validating state occurs when card credentials are being checked; the AccessGranted state occurs when the door unlocks; and the AccessDenied state occurs when the card is rejected. The input symbols are SwipeCard, ValidCard, InvalidCard, PermissionGranted, and PermissionDenied. The accepting state is AccessGranted. The transitions are shown in the transition diagram below.

An example of an input string is SwipeCard [latex]\rightarrow[/latex] ValidCard [latex]\rightarrow[/latex] PermissionGranted, which produces the output string AccessGranted. The FSM accepts this sequence because it ends in the accepting state AccessGranted.