cits2211 discrete structures lectures for semester 2 2017 ......designing a fsm steps for designing...

45
Finite State Machines Nondeterministic FSMS CITS2211 Discrete Structures Lectures for Semester 2 2017 Finite State Machines September 29, 2017

Upload: others

Post on 26-Jan-2021

4 views

Category:

Documents


0 download

TRANSCRIPT

  • Finite State Machines Nondeterministic FSMS

    CITS2211 Discrete StructuresLectures for Semester 2 2017

    Finite State Machines

    September 29, 2017

  • Finite State Machines Nondeterministic FSMS

    What is a computer?

    Studying simple machines allows us to study ideas such as memory,information, nondeterminism, and complexity.

    In week 8 we learned that there are more “problems” then thereare “programs” to solve them since the latter set is countablyinfinite and the former set is uncountable.

    Over the next four weeks we will study models of computation inorder to understand it properties and the fundamental limits.

  • Finite State Machines Nondeterministic FSMS

    Why (study finite state machines)?

    What is a computer? What is computation?To study computers we need a computational modelAny model is accurate in some ways but not othersWe shall start with the simplest model of computation:a Finite State Machine (FSM)

  • Finite State Machines Nondeterministic FSMS

    Why (study finite state machines)?

    Finite State Machines (FSMs) are (essentially) computers withvery small memoryFSMs are widely used in practice for simple mechanisms:automatic doors, lifts, microwave or washing machine controllers,and many other electromechanical devices.FSMs are used also to specify communication protocols, userinterfaces, web forms, and workflows.FSMs are the simplest model in a hierarchy that we will use tounderstand the fundamental question of what is computable.

  • Finite State Machines Nondeterministic FSMS

    Reading

    Introduction to the Theory of Computation by Michael SipserChapter 1: Regular LanguagesSections 1.1 Finite Automata and 1.2 Nondeterminism

  • Finite State Machines Nondeterministic FSMS

    Lecture Outlines

    This week (2 lectures) we will study

    FSMs and FSM diagrams

    FSM formal definition

    How to read a FSM

    FSM computations

    Accepting strings and recognizing languages

    How to design a FSM

    nondeterministic FSMs

    NFSM formal definition

    The surprising and useful equivalence of NFSMs and FSMs

  • Finite State Machines Nondeterministic FSMS

    FSM Diagrams

    A simple model of computation

    Q: What is a computer?

    A: A laptop, PC, phone, tablet, super-computer, ticketmachine, ATM ?

    All of these systems are reactive: their actions depend on theinputs they receive

    Their response to a particular stimulus (a signal, or a piece ofinput) is not the same on every occasion.

  • Finite State Machines Nondeterministic FSMS

    FSM Diagrams

    For example, in the case of a parking ticket machine, it willnot print a ticket when you press the button unless you havealready inserted some money.

    The response to the print button depends on the previoushistory of the use of the system: its memory.

  • Finite State Machines Nondeterministic FSMS

    FSM Diagrams

    Inputs

    What stimuli (inputs) does a ticket machine take account of ?

    insert some money m,

    press the print ticket button t,

    press the cancel button for refunds r

    The alphabet of inputs is a set: Σ = {m, t, r}

  • Finite State Machines Nondeterministic FSMS

    FSM Diagrams

    Inputs

    What stimuli (inputs) does a ticket machine take account of ?

    insert some money m,

    press the print ticket button t,

    press the cancel button for refunds r

    The alphabet of inputs is a set: Σ = {m, t, r}

  • Finite State Machines Nondeterministic FSMS

    FSM Diagrams

    States

    The machine’s memory is represented by a set of states.

    For example1=awaiting coins, 2=ready to print 3=finished printing

    The set of possible states for a given machine is writtenQ = {1, 2, 3}

    States are drawn as circles in FSM diagrams.

    There are only a finite number of possible states allowed for aFinite State Machine (of course!).

  • Finite State Machines Nondeterministic FSMS

    FSM Diagrams

    Transitions

    How does computation occur?

    The machine has transitions from one state to another dependingon the stimulus (input) provided.

    The transition function is of type:

    Q × Σ→ Q

    Transitions are drawn as edges between the states in FSMdiagrams. Edges are labelled with the input symbol for thetransition. For every state, symbol pair there must be a transitionto some other state.To simplify FSM diagrams, we sometimes do not show transitionsfor illegal inputs.

  • Finite State Machines Nondeterministic FSMS

    FSM Diagrams

    Outputs

    In this unit we will not study FSMs with an explicit outputalphabet.

    Outputs (if needed) can be implicit in the states (eg a light beingon while machine is in some state) or the transitions (eg use aunique symbol for each input,output pair)

    If explicit outputs are needed, then they could be written on thetransition edges. A machine with outputs is called a transducer.You may see such models in some texts.

  • Finite State Machines Nondeterministic FSMS

    FSM Diagrams

    Starting and Stopping

    One state from Q is identified as the starting state. Think of thisas the initial state of the machine before any inputs are received.

    The start state is identified by an arrow pointing to it, but notcoming from any other state.

    A machine can stop in any state: input may cease, or there may beno matching transition to take.

    One or more states from Q may be identified as accepting states.These are good places to stop. In diagrams, accepting states aredenoted by a double circle.

  • Finite State Machines Nondeterministic FSMS

    FSM Diagrams

    Ticket Machine example

    1 2 3

    m

    m

    r

    t

  • Finite State Machines Nondeterministic FSMS

    FSM Formal Definition

    Formal defn of FSMs

    Definition: A finite state machine (FSM) is defined to be a5-tuple (Q,Q0,Σ,F , f ) where

    Q is a finite set of states;

    Q0 ∈ Q is the start state;Σ is a finite alphabet of input symbols;

    F ⊆ Q is a set of accepting states (F may be the empty set);f : Q × Σ→ Q is the state transition function

    (Note: You will find slight variations of this definition in some texts. We

    will follow Sipser)

  • Finite State Machines Nondeterministic FSMS

    Computations of a FSM

    Behaviour and Traces

    How do we describe the behaviour of a particular FSM?

    Definition: A trace of an FSM M is a finite sequence of states andtransition labels, starting and ending with a state such that,

    1 the trace is written s0, i1, s1, i2, . . . , in, sn2 the first state, s0 must be the start state of M

    3 every triple, (sj , ij+1, sj+1) must be from the state transitionrelation f of M

    We say s0, i1, s1, i2, . . . , in, sn is a trace on the input string i1i2 . . . in

    Example: The ticket machine has traces 1,m, 2,m, 2,m, 2, t, 3 and1,m, 2,m, 2,m and for input strings mmmt and mmm(respectively)

  • Finite State Machines Nondeterministic FSMS

    Computations of a FSM

    Accepting a String

    We say that an FSM accepts an input string if there is a trace forthat input string which leaves the FSM in an accepting state.Notation: Given an alphabet Σ, the set of all strings formed fromthat alphabet is denoted by Σ∗.

    The ticket machine FSM accepts the strings mmmt and mt andmr but it does not accept mmm and it does not accept t

    1 2 3m

    m

    rt

  • Finite State Machines Nondeterministic FSMS

    Reading Finite State Machines

    Example FSM M1

    Name the five parts of this FSM?List some strings it accepts?

    s0 s1

    1

    0

    0

    1

  • Finite State Machines Nondeterministic FSMS

    Reading Finite State Machines

    Example FSM M2

    1 4

    2

    3

    =

    ==, !

  • Finite State Machines Nondeterministic FSMS

    The Language Recognized by a FSM

    Languages and Recognizers

    The set of strings that are accepted by a FSM is known as thelanguage that is recognized by the FSM.

    Given a finite state machine M, the collection of all strings thatare accepted by M is called the language recognized by M and isdenoted L(M).

    L(M) ⊆ Σ∗

    That is the language recognized by M is a subset of all possiblestrings of its input alphabet.

  • Finite State Machines Nondeterministic FSMS

    The Language Recognized by a FSM

    Example: For the FSM M1 on slide 18

    L(M1) = {w | w ∈ {0, 1}∗ and w ends with a 0}

    Although accept and recognize have similar meanings in English,we will use accept for strings and recognize for the set of all stringsaccepted by a FSM.

    An interesting question is, what languages can be recognized byfinite state machines? And are there languages that can not be?

  • Finite State Machines Nondeterministic FSMS

    The Language Recognized by a FSM

    Language example

    What is the language recognized by this FSM?Hint: Generate some examples of accepted strings.

    0

    1

    2

    3

    4

    a b

    b a

    b

    a b

    a b

    a

  • Finite State Machines Nondeterministic FSMS

    Designing a FSM

    Steps for designing a FSM

    So far we have studied FSMs that already exist. What if you needto write your own?

    Design is a creative process so there is no recipe for it. But hereare some hints to help you.

  • Finite State Machines Nondeterministic FSMS

    Designing a FSM

    Steps for designing a FSM

    1 Pretend that you are the machine.

    2 Generate some example strings: both strings that areaccepted by the machine and those that are not

    3 Step through your example strings one symbol at a time andthink about how the machine would decide to accept or not

    4 What do you need to remember about the string as you arereading it? The things you have to remember will become thestates of the machine.

    5 Work out the transitions between states for each state andsymbol combination.

    6 Set the start state as the state you will be in before you haveseen any symbols

    7 Set the accepting state(s) as the states(s) where you want toaccept a string.

  • Finite State Machines Nondeterministic FSMS

    Designing a FSM

    Designing a FSM

    Example: Define an FSM that accepts only strings of alternating0s and 1s, starting with a 0 and ending with a 1.

  • Finite State Machines Nondeterministic FSMS

    Designing a FSM

    Designing a FSM (1)

    Think of some example stringsaccept: 0101 and 01010101do not accept: 10100 nor 011 nor 1010

    Step through the examples working out what to do at each step0101: see 0 next need 1, next need another 0 (must alternate),then need another 1, accept this alternating string ending in 110101010: see 1 and do not accept because this does not startwith a 00110: see 0, then 1, then do not accept the next 1 because notalternating

  • Finite State Machines Nondeterministic FSMS

    Designing a FSM

    Designing a FSM (2)

    What did we need to remember? Whether we had just seen a 0(then next must be 1), and whether we had seen a 1 (then nextmust be 0) So make 2 states: seen0 and seen1 Transitions: fromseen0 next need a 1, and vice versa

    Starting state? the first symbol must be a 0, which means you arein seen1 state.

    Accepting state(s)? when have just seen a 1 which is the seen1state. This machine has only one accepting state

  • Finite State Machines Nondeterministic FSMS

    Designing a FSM

    Designing a FSM (3)

    seen0seen1

    0

    1

  • Finite State Machines Nondeterministic FSMS

    Designing a FSM

    Here is another possible FSM for accepting exactly strings ofalternating 0s and 1s, starting with a 0 and ending with a 1.How do they differ? Is one any better than the other?

    s0 s1 s20

    1

    0

  • Finite State Machines Nondeterministic FSMS

    Designing a FSM

    Failure to accept a sequence of inputs can arise in two ways:

    We can construct trace(s) corresponding to the sequence, butthe final state of each trace is not an accepting state.e.g. 010 in the above example

    We cannot construct a trace for the given sequencee.g. 0110 in the above example (depending on the actualmachine you constructed).

    The missing sequences are implicitly in the transition function,but not shown in the diagram.

  • Finite State Machines Nondeterministic FSMS

    Designing a FSM

    More design practice

    Example: 1. Design a FSM to accept all binary strings startingwith 111

    Example: 2. Design a FSM to accept all binary strings with evenparity. That is, with an even number of 1s. e.g. 0101 and10111000 but not 100

    Example: 3. Design a FSM to accept all binary strings with thesame number of 0s and 1s, but in any order. e.g. 0101 and1011100010 but not 100

  • Finite State Machines Nondeterministic FSMS

    Designing a FSM

    Maybe, one of those exercises on the previous pages is impossible.

    More on that later.

  • Finite State Machines Nondeterministic FSMS

    Nondeterministic FSM

    q1 q2 q3 q410, � 1

    0, 1 0, 1

    In a NFSM a state may have zero, one or many exiting arrowsfor each alphabet symbol.

    An NFSM may have arrows labelled with the empty symbol �

    Recall that a (deterministic) FSM has exactly one transitionfor each state, symbol pair: transition function

  • Finite State Machines Nondeterministic FSMS

    Nondeterministic FSM

    NFSM computations

    Suppose we run an NFSM on an input string and come to astate with multiple choices for the current input symbol.

    After reading the symbol the machine splits into multiplecopies of itself and follows all the possibilities in parallel

    If there are further choices the machine splits again

    Finally, if any one of the copies of the machine is in an acceptstate at the end of the input, the NFSM accepts the inputstring.

    These multiple possible output choices can be through of as atransition to a set of states (rather than a single state).

  • Finite State Machines Nondeterministic FSMS

    NFSM formal definition

    Formal defn of FSMs

    Definition: A nondeterministic finite state machine (NFSM) isdefined to be a 5-tuple (Q, q0,Σ,F , f ) with empty string symbol �where

    Q is a finite set of states;

    q0 ∈ Q is the start state;Σ is a finite alphabet of input symbols;

    F ⊆ Q is a set of accepting states (F may be the empty set);δ : Q × Σ� → P(Q) is the state transition function

    The transition function in a NFSM takes an input symbol or theempty string and produces a set of possible next states.

  • Finite State Machines Nondeterministic FSMS

    NFSM formal definition

    FSM and NFSM Definitions

    Definition: An input sequence is accepted by an FSM iff at leastone trace for the sequence ends in an accepting state.

    Definition: If there is at most one trace for any input sequence ofFSM M then M is said to be deterministic. (also called DFSM fordeterministic finite-state machine)

    Definition: Any FSM M that is not deterministic, is said to benondeterministic. (NFSM)

  • Finite State Machines Nondeterministic FSMS

    NFSM formal definition

    The power of FSMs

    Essentially an FSM is a model of a computer with an extremelylimited amount of memory. The only mechanism that it has to“remember” something is to move into a different state.

    Despite this, we can produce FSMs that do some surprising things,such as adding two numbers.

    Question: What are the differences are between deterministic andnondeterministic FSMs? Do they recognize the same or differentlanguages ? Why ?

  • Finite State Machines Nondeterministic FSMS

    NFSM formal definition

    NFSM example

    Nondeterminism seems to give the builder great opportunities tobe efficient. We can use the power of guessing.

    Example: Construct a nondeterministic FSM that accepts binarystrings with a 1 in the third last position.

  • Finite State Machines Nondeterministic FSMS

    NFSM formal definition

    NFSM example

    Problem: Construct a NFSM that accepts binary strings with a 1in the third last positionSolution: Guess where the 1 occurs then after 2 more symbols canaccept.

    s0 s1 s2 s31 0, 1 0, 1

    0, 1

  • Finite State Machines Nondeterministic FSMS

    NFSM formal definition

    NFSM example, continued

    Consider how complicated it is to do the same thing with adeterministic FSM.

    Example: Construct a deterministic FSM for the same language.

  • Finite State Machines Nondeterministic FSMS

    NFSM formal definition

    // a

    0,1

    ��1 // b

    0,1 // c0,1 // d

    // a

    0

    ��1 // ab

    1 //

    0

    ��

    abc1 //

    0

    ��

    abcd

    1

    0

    ��

    ac 1 //

    0��

    abd

    1

    OO

    0oo

    ad

    0

    WW

    1

    GG

    acd0

    oo

    1

    OO

    A NFSM with an equivalent DFSM (constructed via the power setconstruction but ignoring unreachable states)

  • Finite State Machines Nondeterministic FSMS

    The surprising and useful equivalence of NFSM and DFSM

    NFSM and DFSM are equivalent

    Theorem: Every NFSM can be converted into an equivalent DFSM

    The powerset construction for this conversion was published byM.O. Rabin and Dana Scott in 1959.

    Given an NFSM we construct an equivalent DFSM as follows.

    1 states are P(Q) (the power set of states of the NFSM)2 initial state is the state corresponding to the union of initial

    states in the NFSM

    3 accepting states are those containing any accepting states inthe NFSM

    4 transition function maps each compound state to its matchingone

  • Finite State Machines Nondeterministic FSMS

    The surprising and useful equivalence of NFSM and DFSM

    Applications

    That NFSM and DFSM are equivalent is surprising:“guessing” and “calculating” have the same power.This equivalence is very useful in practice:

    NFSMs are often easier to construct than DFSMs, so solve aproblem with an NFSM and then convert it to a DFSM forexecution

    However, note for an NFSM with n states, the DFSM mayhave up to 2n states

    NFSMs are used for easier proofs of properties, such asclosure, in the theory of computation:

  • Finite State Machines Nondeterministic FSMS

    Summary

    1 Finite State Machines (FSMs): a simple model ofcomputation with limited memory

    2 Acceptors: An FSM accepts a string if that string of inputsymbols takes the FSM to an accepting state

    3 Recognizers: An FSM recognises the language comprising theset of all the strings it accepts.

    4 Nondeterministic FSMs: nondeterministic (guessing) FSMshave the same power as deterministic (calculating) ones.

    Finite State MachinesFSM DiagramsFSM Formal DefinitionComputations of a FSMReading Finite State MachinesThe Language Recognized by a FSMDesigning a FSM

    Nondeterministic FSMSNondeterministic FSMNFSM formal definitionThe surprising and useful equivalence of NFSM and DFSM