An entire collection of useful table-driven algorithms makes use of a theoretical
concept known as a finite state machine (FSM).
Example Algorithm
Input a stream of “bits” (‘0’ or ‘1’ characters) from a text stream.
Output to the standard output stream, according to the following rules:
1) Write an ‘X’ for every odd-numbered bit (i.e., 1st, 3rd, 5th, etc.)
2) Write a ‘Z’ for every even-numbered bit with value of ‘0’.
3) Write an ‘N’ for every even-numbered bit with value of ‘1’.
Sample
input stream
resulting output
0 0 0 1 1 1 1 0 0 1
X Z
Question
How could we draw a picture of this algorithm?
The Object of Data Abstraction
and Structure, David D. Riley
© Addison Wesley pub.
A graphical model for the preceding algorithm is shown below.
1 / X
After
Even
0 / X
After
Odd
0 / Z
1 / N
Notation
Each circle is a separate __________.
One state has an incoming arc without a state on the opposite end.
This state is called the _________________________.
Each arc represents a potential transition from one state to another.
The label a / b denotes an input symbol of a and output of b.
The Object of Data Abstraction
and Structure, David D. Riley
© Addison Wesley pub.
1 / X
After
Even
0 / X
After
Odd
0 / Z
1 / N
Sample
input stream
0 0 0 1 1 1 1 0 0 1
resulting output
The Object of Data Abstraction
and Structure, David D. Riley
© Addison Wesley pub.
An (Finite State Machine) FSM is a 6-tuple:
1) A set of states
2) A start state
3) A set of input symbols
4) A set of output symbols
5) A next state function
( State Input State)
6) An output function
( State Input Output)
Name the parts below
After
Even
1 / X
0 / X
After
Odd
NextState(AfterEven, 0) AfterOdd
NextState(AfterEven, 1) AfterOdd
NextState(AfterOdd, 0) AfterEven
NextState(AfterOdd, 1) AfterEven
Output(AfterEven, 0) X
Output(AfterEven, 1) X
Output(AfterOdd, 0) Z
Output(AfterOdd, 1) N
Actions other than output
are also permitted in some
FSMs.
0 / Z
1 / N
The Object of Data Abstraction
and Structure, David D. Riley
© Addison Wesley pub.
0 / 0
How many states?
1 / 1
Which is the start state?
What is the input alphabet?
What is the output alphabet?
0 / 0
1 / 3
Select meaningful state labels.
0 / 2
1 / 3
The Object of Data Abstraction
and Structure, David D. Riley
© Addison Wesley pub.
A 4-Step Strategy
1) Select the states.
2) Identify the start state.
3) Complete the Next State function.
4) Complete the Output function.
The Object of Data Abstraction
and Structure, David D. Riley
© Addison Wesley pub.
Input consists of individual bits (0 or 1).
Input is partitioned into packets consisting of four consecutive bits.
A dash (-) is output for each of the first three bits in a packet.
An ‘E’ is output for each packet that has even parity.
A ‘D’ is output for each packet that has odd parity.
(Note that even parity occurs when the count of 1-valued bits in
the packet is even, and odd parity occurs when the count is odd.)
Sample
input stream
resulting output
0 0 0 1 0 0 0 0 0 1
1
1
-
-
D
-
-
D -
-
-
E -
-
The Object of Data Abstraction
and Structure, David D. Riley
© Addison Wesley pub.
A 4-Step Strategy
1) Select the states.
2) Identify the start state.
3) Complete the Next State function.
What is a state?
A state is a kind of memory.
A state “remembers” the
most recent input(s).
4) Complete the Output function.
A state “remembers” a
count.
What does the 4-bit parity checker need to remember?
Hints for Selecting States
Partition the algorithm into all possible situations (states).
Try tracing FSM execution until states appear to repeat.
What are the states of a 4-bit parity checker?
The Object of Data Abstraction
and Structure, David D. Riley
© Addison Wesley pub.
A 4-Step Strategy
1) Select the states.
2) Identify the start state.
3) Complete the Next State function.
4) Complete the Output function.
What is the start state of the 4-bit parity checker?
The Object of Data Abstraction
and Structure, David D. Riley
© Addison Wesley pub.
A 4-Step Strategy
1) Select the states.
2) Identify the start state.
3) Complete the Next State function.
Think of the NextState
function as a table.
4) Complete the Output function.
input symbol
previous state
0
1
The Object of Data Abstraction
and Structure, David D. Riley
© Addison Wesley pub.
A 4-Step Strategy
1) Select the states.
2) Identify the start state.
3) Complete the Next State function.
Think of the Output
function as a table.
4) Complete the Output function.
input symbol
previous state
0
1
NoBits
Even1
Odd1
Even2
Odd2
Even3
Odd3
The Object of Data Abstraction
and Structure, David D. Riley
© Addison Wesley pub.
Using table-driven code for the NextState and Output functions, makes
it possible to create a general-purpose FSM simulator.
currentState = startState();
input = readInputSymbol();
while (! endOfInput() ) {
performOutput( outputTable[currentState][input] );
currentState = nextStateTable[currentState][input];
input = readInputSymbol();
}
The Object of Data Abstraction
and Structure, David D. Riley
© Addison Wesley pub.
© Copyright 2026 Paperzz