Presentation is loading. Please wait.

Presentation is loading. Please wait.

An entire collection of useful table-driven algorithms makes use of a theoretical concept known as a finite state machine (FSM). Example Algorithm Input.

Similar presentations


Presentation on theme: "An entire collection of useful table-driven algorithms makes use of a theoretical concept known as a finite state machine (FSM). Example Algorithm Input."— Presentation transcript:

1 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 X 001111001 Z Question How could we draw a picture of this algorithm? The Object of Data Abstraction and Structure, David D. Riley © Addison Wesley pub.

2 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.

3 Sample input stream  resulting output  0001111001 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.

4 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) 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 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.

5 How many states? 0 / 0 1 / 1 0 / 2 1 / 3 0 / 0 0 / 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.

6 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.

7  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 - 001000001 --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.

8 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.

9 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.

10 Think of the NextState function as a table. 01 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.

11 Think of the Output function as a table. 01 input symbol previous state NoBits Even1 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.

12 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.


Download ppt "An entire collection of useful table-driven algorithms makes use of a theoretical concept known as a finite state machine (FSM). Example Algorithm Input."

Similar presentations


Ads by Google