Presentation is loading. Please wait.

Presentation is loading. Please wait.

Finite-State Machines (FSM) Chuck Cusack Based partly on Chapter 11 of “Discrete Mathematics and its Applications,” 5 th edition, by Kenneth Rosen.

Similar presentations


Presentation on theme: "Finite-State Machines (FSM) Chuck Cusack Based partly on Chapter 11 of “Discrete Mathematics and its Applications,” 5 th edition, by Kenneth Rosen."— Presentation transcript:

1 Finite-State Machines (FSM) Chuck Cusack Based partly on Chapter 11 of “Discrete Mathematics and its Applications,” 5 th edition, by Kenneth Rosen

2 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

3 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

4 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 finite- state 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.

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

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

7 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

8 Candy Machine FSM We now have enough information to construct our finite- state 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.

9 Candy Machine State Diagram

10 Candy Machine State Table Next StateOutput Input State51025CBSB51025CBSB S0S0 S1S1 S2S2 S5S5 S0S0 S0S0 nnnnn S1S1 S2S2 S3S3 S5S5 S1S1 S1S1 nn5nn S2S2 S3S3 S4S4 S5S5 S2S2 S2S2 nn10nn S3S3 S4S4 S5S5 S5S5 S3S3 S3S3 nn15nn S4S4 S5S5 S5S5 S5S5 S0S0 S4S4 n520 Candy n S5S5 S5S5 S5S5 S5S5 S0S0 S0S0 51025 Candy,5Soda

11 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 state- input pair to an output –S 0 is the initial state

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

13 State Tables A state table is organized as follows Output Input 51025CBSB nnnnn nn5nn nn10nn nn15nn n520 Candy n 51025 Candy,5Soda State S0S0 S1S1 S2S2 S3S3 S4S4 S5S5 Next State Input 51025CBSB S1S1 S2S2 S5S5 S0S0 S0S0 S2S2 S3S3 S5S5 S1S1 S1S1 S3S3 S4S4 S5S5 S2S2 S2S2 S4S4 S5S5 S5S5 S3S3 S3S3 S5S5 S5S5 S5S5 S0S0 S4S4 S5S5 S5S5 S5S5 S0S0 S0S0 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. –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

14 State Diagram A state diagram is organized as follows –The nodes in the graph represent the states. – Each edge is labeled with a pair (x,y), where – An edge (S i,S j ) occurs if some input causes a transition from S i to S j – The edges in the graph represent the transitions. x is the input which (along with the state) causes the transition y is the output triggered by the state-input pair.

15 Example: Unit Delay In some electronic devices, it is necessary to use a unit- delay 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, …, x n, the machine should produce the string 0,x 1, x 2, …, x n-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.

16 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)

17 Unit Delay State Table/Diagram Here is the state table and state diagram based on our previous observations. Next StateOutput Input State0101 S0S0 S2S2 S1S1 00 S1S1 S2S2 S1S1 11 S2S2 S2S2 S1S1 00

18 Example: Binary Adder We want to construct a finite state machine that will add two numbers. The input is two binary numbers, (x n …x 1 x 0 ) 2 and (y n …y 1 y 0 ) 2 At each step, we can compute (x i +y i ) starting with (x 0 +y 0 ). –If (x i +y i )=0, we output 0. –If (x i +y i )=1, we output 1. –If (x i +y i )=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 x i +y i +c i 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.

19 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 (x i +y i )=0, we output 0, and stay in state S 0 –If (x i +y i )=1, we output 1, and stay in state S 0 –If (x i +y i )=2, we output 0, and go to state S 1 If we are in state S 1 (we have a carry of 1) –If (x i +y i +1)=1, we output 1, and go to state S 0 –If (x i +y i +1)=2, we output 0, and stay in state S 1 –If (x i +y i +1)=3, we output 1, and stay in state S 1

20 Binary Adder State Table/Diagram From the previous observations, we can construct the state table and state diagrams for the binary adder Next StateOutput Input State0001101100011011 S0S0 S0S0 S0S0 S0S0 S1S1 0110 S1S1 S0S0 S1S1 S1S1 S1S1 1001

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

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

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

24 Dealing with Input Strings Let x=x 1 x 2 …x n    (That is, x is a string over I) Then we can extend the transition function f to all state- input 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 ) = S i 2 –Next compute f(S i 2, x 2 ) = S i 3 –Continue until you get f(S i n, x n ) = S i n+1 –We define f(S 0, x) = S i n+1 From now on, we will speak of the transition function f being applied to input strings, not just single inputs.

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

26 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,…}

27 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}

28 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  0A, A  0A, A  1A, 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.

29 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 finite- state automaton. In other words, regular sets, regular languages, and languages recognized by finite-state automata are all the same thing.

30 A More Complex Example Example: What language does the following finite-state automaton recognize?

31 Complex Example Continued If start by going to state S 1 can recognize 000, 0110, 011100, 0111110, 011111100, 00100, 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 1s. These are all of the form 0(111  01)*(00  110)1* But we can also start by going to S 6

32 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 0s as we want These are of the form 1(01)*10* Thus, we can recognize (0(111  01)*(00  110)1*)  1(01)*10*)

33 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?

34 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


Download ppt "Finite-State Machines (FSM) Chuck Cusack Based partly on Chapter 11 of “Discrete Mathematics and its Applications,” 5 th edition, by Kenneth Rosen."

Similar presentations


Ads by Google