an entire collection of useful table-driven algorithms makes use of a theoretical concept known as a...
TRANSCRIPT
An entire collection of useful table-driven algorithms makes use of a theoretical concept known as a finite state machine (FSM).
Example AlgorithmInput 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
X
0 0 1 1 1 1 0 0 1
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.
After Even
After Odd
1 / X
0 / X
0 / Z
1 / N
Notation Each circle is a separate __________.
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.
One state has an incoming arc without a state on the opposite end. This state is called the _________________________.
The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.
Sample input stream
resulting output
0 0 0 1 1 1 1 0 0 1
After Even
After Odd
1 / X
0 / X
0 / Z
1 / N
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
states2) A start state3) A set of input symbols4) A set of output symbols5) A next state function
( State Input State)6) An output function
( State Input Output)
NextState(AfterEven, 0) AfterOdd NextState(AfterEven, 1) AfterOddNextState(AfterOdd, 0) AfterEvenNextState(AfterOdd, 1) AfterEven
Output(AfterEven, 0) XOutput(AfterEven, 1) XOutput(AfterOdd, 0) ZOutput(AfterOdd, 1) N
After Even
After Odd
1 / X
0 / X
0 / Z
1 / N
Name the parts below
Actions other than output are also permitted in some FSMs.
The Object of Data Abstraction and Structure, David D. Riley© Addison Wesley pub.
How many states?
0 / 0
1 / 1
0 / 2
1 / 3
1 / 30 / 00 /
2
1 / 1
Which is the start state?
What is the input alphabet? What is the output alphabet? Select meaningful state labels.
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.
Sample
input stream resulting output
0
-
0 0 1 0 0 0 0 0 1
- - D - - - E - -
1
-
1
D
(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.)
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 a state?A state is a kind of
memory. A state “remembers” the most recent input(s). 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.
What is the start state of the 4-bit parity checker?
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.
Think of the NextState function as a table.
0 1
input symbol
previous state
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.
Think of the Output function as a table.
0 1
input symbol
previous state
NoBitsEven1
Odd1
Even2
Odd2
Even3
Odd3
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.
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.