FiniteState Machines FSM Chuck Cusack Based partly on
Finite-State Machines (FSM) Chuck Cusack • Based partly on Chapter 11 of “Discrete Mathematics and its Applications, ” 5 th edition, by Kenneth Rosen
Types of Finite-State Machines • Finite-State Machines with Output – Mealy: Output determined by state and input – Moore: Output determined by state alone • Finite-State Machines with No Output – Also known as finite-state automata – There are two types of finite-state automata • Deterministic: Each state-input pair dictates a unique transition into another state • Non-deterministic: Each state-input pair can lead to several possible states
Applications • Finite-State Machines are used in a variety of applications. – Spell checking programs – Grammar checking – Indexing and searching large text files – Speech/Language recognition – Network Protocols
Finite State Machines with Output • We will focus on only Mealy machines in these notes. • Since we will always refer to finite-state machines with no output as finite-state automata, we will use the term finite-state machine to mean finitestate machine with output. • The best way to understand finite-state machines is probably with an example. • Perhaps the best example deals with a device most of use are very familiar with—vending machines.
Example: Candy Machine • Consider a vending machine that – Accepts nickels (5 cents), dimes (10 cents), and quarters (25 cents), crediting the amount. – If the total credit is more than 25, it returns the difference so only 25 cents credit remains. – Dispenses a candy bar if the candy button is pushed and there is 20 cents credit. – Dispenses a candy bar and returns 5 cents if the candy button is pushed and there is 25 cents credit. – Dispenses a soda if the soda button is pushed and there is 25 cents credit.
Candy Machine States • The vending machine can be in different states based on the amount of money that has been credited to the user. • Change is returned after 25 cents, and all coins are multiples of 5. • Thus, the machine can be in the following states: – – – 0 cents credit (state S 0) 5 cents credit (state S 1) 10 cents credit (state S 2) 15 cents credit (state S 3) 20 cents credit (state S 4) 25 cents credit (state S 5)
Candy Machine Input/Output • The machine can accept the following inputs – – – A dime (10 cents) inserted A nickel (5 cents) inserted A quarter (25 cents) inserted Candy button pushed (CB) Soda button pushed (SB) • The machine has the following possible outputs – – – A dime (10 cents) returned A nickel (5 cents) returned A quarter (25 cents) returned A candy bar (C) dispensed A soda (S) dispensed Nothing (n) is returned or dispensed
Candy Machine FSM • We now have enough information to construct our finitestate machine. • For each possible input and each possible state, we need to know what to output (if anything) and what state the machine should go to. • For instance: – If the machine is in state S 3 (15 cents credit) and – a quarter (25 cents) is input – the machine should transition into state S 5 (25 cents credit) and – 15 cents (a dime and nickel) should be output. • We can construct a state diagram and/or a state table by considering every possible input on every possible state.
Candy Machine State Diagram
Candy Machine State Table Next State Input State 5 10 25 CB S 0 S 1 S 2 S 5 S 0 S 1 S 2 S 3 S 5 S 1 S 2 S 3 S 4 S 5 S 5 S 0 S 5 S 5 S 0 SB S 0 S 1 S 2 S 3 S 4 S 0 5 n n n 5 Output Input 10 25 CB n n 5 n n 10 n n 15 n 5 20 Candy 10 25 Candy, 5 SB n n n Soda
FSM Definition • Definition: A finite-state machine is a 6 tuple M=(S, I, O, f, g, S 0) where – S is a finite set of states – I is a finite input alphabet – O is a finite output alphabet – f: S´I S is a transition function from each state -input pair to a state – g: S´I O is a output function from each stateinput pair to an output – S 0 is the initial state
FSM Representations • As we have already seen, there are two common ways of representing finite-state machines – A state table is used to represent a finite-state machine by giving the values of the functions f and g. – A state diagram is a directed graph representation of a finite-state machine.
State Tables • A state table is organized as follows –The rows are indexed by the states. –The columns are split into two groups: • The first half are indexed by the inputs • The entries in the table give the value of the function f – that is the new states Next State Output Input State 5 10 25 CB SB S 0 S 1 S 2 S 5 S 0 n n n S 1 S 2 S 3 S 5 S 1 n n 5 n n S 2 S 3 S 4 S 5 S 2 n n 10 n n S 3 S 4 S 5 S 3 n n 15 n n S 4 S 5 S 5 S 0 S 4 n 5 20 Candy n S 5 S 5 S 0 5 10 25 Candy, 5 Soda • The second half are also indexed by the inputs • The entries in the table give the value of the function g – that is, the outputs.
State Diagram • A state diagram is organized as follows – The nodes in the graph represent the states. – The edges in the graph represent the transitions. – An edge (Si, Sj) occurs if some input causes a transition from Si to Sj – Each edge is labeled with a pair (x, y), where • x is the input which (along with the state) causes the transition • y is the output triggered by the state-input pair.
Example: Unit Delay • In some electronic devices, it is necessary to use a unitdelay machine. • That is, whatever is input into the machine should be output from the machine, but delayed by a specific amount of time. • For instance, given a string of binary numbers x 1, x 2, …, xn, the machine should produce the string 0, x 1, x 2, …, xn-1. • We want to use a finite state machine to model the behavior of a unit-delay machine. • What should a state in this machine represent? • One possibility is that a state represents the last input bit. • Thus we need a state for “ 1” and a state for “ 0” • We also need start state.
Unit Delay States • We will use the following states – State S 0 is the start state – State S 1 occurs if the previous input was 1 – State S 2 occurs if the previous input was 0 • We can easily construct the state table for the unit delay machine by realizing that – When the input is 0, we always transition to state S 2 – When the input is 1, we always transition to state S 1 – When we are in state S 1 we always output 1 (since the previous input was 1) – When we are in state S 2 we always output 0 (since the previous input was 0) – When we are in state S 0 we always output 0 (since we always output 0 first)
Unit Delay State Table/Diagram • Here is the state table and state diagram based on our previous observations. Next State Input Output Input State S 0 0 S 2 1 S 1 0 0 1 0 S 1 S 2 S 2 S 1 1 0
Example: Binary Adder • We want to construct a finite state machine that will add two numbers. • The input is two binary numbers, (xn…x 1 x 0)2 and (yn…y 1 y 0)2 • At each step, we can compute (xi+yi) starting with (x 0+y 0). – If (xi+yi)=0, we output 0. – If (xi+yi)=1, we output 1. – If (xi+yi)=2, we have a problem. • The problem is we need a carry bit. • In fact, our computation needs to know the carry bit at each step (so we compute xi+yi+ci at each step), and be able to give it to the next step. • We can take care of this by using states to represent the carry bit.
Binary Adder States • We will use the following states – State S 0 occurs if the carry bit is 0 – State S 1 occurs if the carry bit is 1 • Since when we begin the computation, there is no carry, we can use S 0 as the start state, • So, how does which state we are in affect the output? • If we are in state S 0 (we have a carry of 0) – If (xi+yi)=0, we output 0, and stay in state S 0 – If (xi+yi)=1, we output 1, and stay in state S 0 – If (xi+yi)=2, we output 0, and go to state S 1 • If we are in state S 1 (we have a carry of 1) – If (xi+yi +1)=1, we output 1, and go to state S 0 – If (xi+yi +1)=2, we output 0, and stay in state S 1 – If (xi+yi +1)=3, we output 1, and stay in state S 1
Binary Adder State Table/Diagram • From the previous observations, we can construct the state table and state diagrams for the binary adder Next State Output Input State 00 01 10 11 S 0 S 0 S 1 0 1 1 0 S 1 S 1 S 1 1 0 0 1
Finite-State Automata • Nondeterministic finite-state automata are important for proving theorems about finite-state machines, grammars, and regular expressions. • However, deterministic and nondeterministic finite -state automata are equivalent in a very important sense. • Since we will not be proving any theorems, we therefore have no need in this context to discuss nondeterministic finite-state automata. • Thus, we will focus on only deterministic finite state automata in these notes.
Finite-State Automata • Definition: A finite-state automaton is a 5 -tuple M=(S, I, f, S 0, F) where – S is a finite set of states – I is a finite input alphabet – f: S´I S is a transition function from each state-input pair to a state – S 0 is the initial state – FÍS is a set of final states • Note: automaton is the singular of automata.
Representations • As with finite-state machines, finite-state automata have the following common representations – A state table is used to represent a finite-state automaton by giving the values of the function f. • Just like for finite-state machines, except the second half of the columns are omitted, since there is no output. – A state diagram is a directed graph representation of a finite-state automaton. • Final states are usually denoted by double-circles. • Values separated by commas denote several possible inputs, not inputs and outputs as with finite state machines.
Dealing with Input Strings • Let x=x 1 x 2…xnÎI * (That is, x is a string over I) • Then we can extend the transition function f to all stateinput string pairs (rather than simply state-input pairs) in the obvious way – – – Assume the machine is in state S 0, Compute f(S 0, x 1) = Si 2 Next compute f(Si 2, x 2) = Si 3 Continue until you get f(Sin, xn) = Sin+1 We define f(S 0, x) = Sin+1 • From now on, we will speak of the transition function f being applied to input strings, not just single inputs.
Language Recognition • Definition: A finite-state automaton accepts (or recognizes) a string x if f(S 0, x)ÎF. That is, the finite state automaton ends up in a final state. • Definition: The language accepted (or recognized) by a finite-state automaton M, denoted by L(M), is the set of all strings recognized by M. • Definition: Two finite-state automata are equivalent if they recognize the same language.
What is the Language? • Example: What language is recognized by the following finite-state automaton? • Solution: Since the only final state is the start state, and only an input of 1 will leave the machine in the start state, it is easy to see that L(M)={1 n: n=0, 1, 2, …}
What is the Language? • Example: What language is recognized by the following finite-state automaton? • Solution: Notice that – Any input that does not start with 0 cannot go to a final state – The final state can only be arrived at if the last input is a 1 – Any string can occur in between the first 0 and last 1. • Thus, we can see that L(M)={binary strings beginning with 0 and ending with 1}
Grammars, Expressions, and Automata • Consider the set A={binary strings which start with 0 and end with 1} – We just saw that A is recognized by a finite-state automata. – In an earlier set of notes, we saw that A was generated by the grammar with V={S, A, 0, 1}, T={0, 1}, and P={S 0 A, A 1 A, A 1} – We also saw that A is defined by the regular expression 0(0È1)*1 • This is no coincidence, as we will see next.
Grammars, Expressions, and Automata • Theorem: Let L be a language. The following three statements are equivalent – L is regular set (that is, L generated by a regular expression) – L is a regular language (that is, L generated by a regular grammar) – L is recognized by a finite-state automaton • Put another way, L is a regular set if and only if L is a regular language if and only if L is recognized by a finitestate automaton. • In other words, regular sets, regular languages, and languages recognized by finite-state automata are all the same thing.
A More Complex Example • Example: What language does the following finite-state automaton recognize?
Complex Example Continued • If start by going to state S 1 can recognize 000, 011100, 011111100, 0010100, 01110110, 01110100, … • It is not easy to see the pattern right away, but notice that they – – Start with 0 Can have any number of instances of 111 or 01 interleaved Can then have either 00 or 110 Can end with any number of 1 s. • These are all of the form 0(111È01)*(00È110)1* • But we can also start by going to S 6
Complex Example Continued • If we start by going to S 6 we notice that the strings – – Start with 1 Have any number of occurrences of 01 Have a 1 End with as many 0 s as we want • These are of the form 1(01)*10* • Thus, we can recognize (0(111È01)*(00È110)1*) È (1(01)*10*)
Limitations • Problem: Find a finite-state automaton that recognizes the following language – L={0 n 1 n | n=0, 1, 2, …} • Solution: It cannot be done. • Proof: Take an advanced course. • Can you describe L with a regular expression? • Can you give a regular grammar that generates L? • Can you give any grammar that generates L?
Summary • Hopefully it is clear that although finite-state machines and finite-state automata are useful models of computation, they have serious limitations. • Are there more powerful ways to model computation? • The answer is: Yes. • Some more powerful models include – – Pushdown automaton Linear bounded automaton Turing machines Quantum computation models
- Slides: 34