Presentation is loading. Please wait.

Presentation is loading. Please wait.

Finite State Machines Dr K R Bond 2009

Similar presentations


Presentation on theme: "Finite State Machines Dr K R Bond 2009"— Presentation transcript:

1 Finite State Machines Dr K R Bond 2009
NEW A2 COMPUTING Finite State Machines Dr K R Bond 2009 © Dr K R Bond 2009

2 NEW A2 COMPUTING Why finite state machines are useful
Many kinds of task can be modelled using a structure called a finite state machine. Finite state machines (FSMs) are used extensively in applications in computer science. For example, finite state machines are the basis for any kind of controller, e.g. traffic lights © Dr K R Bond 2009

3 NEW A2 COMPUTING Programs for
Specifying a language, eg given any string, an FSM determines if that string is in the language or not Programs for spell checking grammar checking indexing or searching large bodies of text recognising speech processing text containing mark-up languages such as XML and HTML Networking protocols that specify how computers communicate. © Dr K R Bond 2009

4 NEW A2 COMPUTING An FSM is used when searching for words in a large piece of text that match a given pattern such as br*v*, where the * can match any sequence of non-blank characters. The FSM would find matches, for example, in brave, bravo and brevity. © Dr K R Bond 2009

5 NEW A2 COMPUTING s a | b b | a t
The a part of the label is called the transition’s trigger and denotes the input symbol An edge leading from state s to state t is called a transition and is labelled with a symbolic code, e.g. a | b s a | b b | a t The b part which is optional denotes the output symbol State transition diagram: a directed graph whose nodes represent the states State transition diagram: a directed graph whose nodes represent the states. An edge leading from state s to state t is called a transition and is labelled with a symbolic code, e.g. a | b. The a part of the label is called the transition’s trigger and denotes the input symbol. The b part which is optional denotes the output symbol. State transition diagram for a Finite State Machine with outputs © Dr K R Bond 2009

6 NEW A2 COMPUTING A finite state machine (FSM) has a set of input symbols (input symbol alphabet) If it produces output, it has a set of output symbols (output symbol alphabet). The FSM in the figure has an input alphabet consisting of the symbols a and b and an output alphabet consisting of the symbols a and b. State transition diagram: a directed graph whose nodes represent the states. An edge leading from state s to state t is called a transition and is labelled with a symbolic code, e.g. a | b. The a part of the label is called the transition’s trigger and denotes the input symbol. The b part which is optional denotes the output symbol. © Dr K R Bond 2009

7 NEW A2 COMPUTING We can express this using set notation as follows:
Input alphabet = {a, b} Output alphabet = {a, b} {} enclose the symbols which are members of the set. Using this input alphabet, input strings to this FSM can be any combination of a’s and b’s, e.g. aabbaab, bbaa, ba. ……, bbb State transition diagram: a directed graph whose nodes represent the states. An edge leading from state s to state t is called a transition and is labelled with a symbolic code, e.g. a | b. The a part of the label is called the transition’s trigger and denotes the input symbol. The b part which is optional denotes the output symbol. © Dr K R Bond 2009

8 NEW A2 COMPUTING The FSM has a finite set of states, {s, t}.
The FSM has a transition function that maps a state-symbol pair (current state and input symbol) to a state (next state) and possibly generates an output (output symbol) depending on the type of FSM. We can use a transition table to represent this transition function. State transition diagram: a directed graph whose nodes represent the states. An edge leading from state s to state t is called a transition and is labelled with a symbolic code, e.g. a | b. The a part of the label is called the transition’s trigger and denotes the input symbol. The b part which is optional denotes the output symbol. © Dr K R Bond 2009

9 NEW A2 COMPUTING Current state s t Input symbol a b Next state
Output symbol State transition diagram: a directed graph whose nodes represent the states. An edge leading from state s to state t is called a transition and is labelled with a symbolic code, e.g. a | b. The a part of the label is called the transition’s trigger and denotes the input symbol. The b part which is optional denotes the output symbol. © Dr K R Bond 2009

10 NEW A2 COMPUTING Transition function:
maps (input symbol, current state)  (output symbol, next state) Transition table: lists the mappings in tabular form (input symbol, current state)  (output symbol, next state) State transition diagram: a directed graph whose nodes represent the states. An edge leading from state s to state t is called a transition and is labelled with a symbolic code, e.g. a | b. The a part of the label is called the transition’s trigger and denotes the input symbol. The b part which is optional denotes the output symbol. © Dr K R Bond 2009

11 NEW A2 COMPUTING The FSM has one state which is designated as the start or initial state. This is indicated by the symbol on the state transition diagram. State transition diagram: a directed graph whose nodes represent the states. An edge leading from state s to state t is called a transition and is labelled with a symbolic code, e.g. a | b. The a part of the label is called the transition’s trigger and denotes the input symbol. The b part which is optional denotes the output symbol. © Dr K R Bond 2009

12 NEW A2 COMPUTING Thus starting in state s, for input aabba the output generated by the FSM is bbaab. Satisfy yourself that the purpose of this FSM is to convert a’s into b’s and b’s into a’s. s a | b b | a t State transition diagram: a directed graph whose nodes represent the states. An edge leading from state s to state t is called a transition and is labelled with a symbolic code, e.g. a | b. The a part of the label is called the transition’s trigger and denotes the input symbol. The b part which is optional denotes the output symbol. © Dr K R Bond 2009

13 NEW A2 COMPUTING Remember
A finite-state machine represents a system as a set of states, the transitions between the states along with the associated inputs and outputs drawn from the machine’s alphabet of symbols. One state is designated the start state. State transition diagram: a directed graph whose nodes represent the states. An edge leading from state s to state t is called a transition and is labelled with a symbolic code, e.g. a | b. The a part of the label is called the transition’s trigger and denotes the input symbol. The b part which is optional denotes the output symbol. © Dr K R Bond 2009

14 NEW A2 COMPUTING To prevent ambiguity as to the machine’s next move
(i.e. in order that its behaviour is deterministic) we require that every state has exactly one transition or edge leaving it for each of the symbols in the input symbol alphabet. State transition diagram: a directed graph whose nodes represent the states. An edge leading from state s to state t is called a transition and is labelled with a symbolic code, e.g. a | b. The a part of the label is called the transition’s trigger and denotes the input symbol. The b part which is optional denotes the output symbol. © Dr K R Bond 2009

15 NEW A2 COMPUTING s a | b b | a t
A state transition diagram for which there is more than one outgoing transition or edge for the same trigger This diagram describes an FSM which is nondeterministic because it contains an ambiguity. s a | b b | a t State s has two transitions labelled with the trigger a State transition diagram: a directed graph whose nodes represent the states. An edge leading from state s to state t is called a transition and is labelled with a symbolic code, e.g. a | b. The a part of the label is called the transition’s trigger and denotes the input symbol. The b part which is optional denotes the output symbol. The FSM has two choices to exit state s when the input symbol is a © Dr K R Bond 2009

16 NEW A2 COMPUTING There is another way that an FSM can be nondeterministic. This occurs when for one or more input symbols in the input symbol alphabet there is no matching transition from a state. s a b t To see why this is nondeterministic, consider the situation when the FSM is in state s and the next input symbol is b. There is no outgoing transition for this case State transition diagram: a directed graph whose nodes represent the states. An edge leading from state s to state t is called a transition and is labelled with a symbolic code, e.g. a | b. The a part of the label is called the transition’s trigger and denotes the input symbol. The b part which is optional denotes the output symbol. © Dr K R Bond 2009

17 NEW A2 COMPUTING States that have no outgoing transitions are called halting states. If the FSM has not exhausted the input when it finds itself in halting state then it will usually report an error. It will usually report an error if it is in a state with outgoing transitions but none of these has a trigger which matches the current input symbol State transition diagram: a directed graph whose nodes represent the states. An edge leading from state s to state t is called a transition and is labelled with a symbolic code, e.g. a | b. The a part of the label is called the transition’s trigger and denotes the input symbol. The b part which is optional denotes the output symbol. © Dr K R Bond 2009

18 (input symbol, current state)  (action, next state)
NEW A2 COMPUTING The FSM may be constructed to perform an action at the output stage instead of outputting a symbol: If a match is found for the pair (input symbol, current state) then an associated action function will be called and the current state will be set to the next state Time displayed Date displayed button a display pressed date pressed time State transition diagram: a directed graph whose nodes represent the states. An edge leading from state s to state t is called a transition and is labelled with a symbolic code, e.g. a | b. The a part of the label is called the transition’s trigger and denotes the input symbol. The b part which is optional denotes the output symbol. (input symbol, current state)  (action, next state) © Dr K R Bond 2009

19 (input signal, current state)  (action, next state)
NEW A2 COMPUTING (input signal, current state)  (action, next state) Time displayed Date displayed button a display pressed date pressed time A digital watch with a button a which is pressed to switch state The input symbol is replaced by a signal which signals the pressing of button a event State transition diagram: a directed graph whose nodes represent the states. An edge leading from state s to state t is called a transition and is labelled with a symbolic code, e.g. a | b. The a part of the label is called the transition’s trigger and denotes the input symbol. The b part which is optional denotes the output symbol. © Dr K R Bond 2009

20 NEW A2 COMPUTING Finite State Automata
A finite state automaton (FSA) or finite automaton is a finite state machine that produces no output whilst processing the input. Finite state automata (plural of finite state automaton) solve decision problems, i.e. problems where the outcome is either YES or NO. State transition diagram: a directed graph whose nodes represent the states. An edge leading from state s to state t is called a transition and is labelled with a symbolic code, e.g. a | b. The a part of the label is called the transition’s trigger and denotes the input symbol. The b part which is optional denotes the output symbol. © Dr K R Bond 2009

21 NEW A2 COMPUTING A finite state automaton simply runs through the input sequence of symbols, one by one, changing states as a result of the current state and the current symbol from the input it sees. State transition diagram: a directed graph whose nodes represent the states. An edge leading from state s to state t is called a transition and is labelled with a symbolic code, e.g. a | b. The a part of the label is called the transition’s trigger and denotes the input symbol. The b part which is optional denotes the output symbol. © Dr K R Bond 2009

22 NEW A2 COMPUTING a b a or b a or b
Finite state automaton with an alphabet of {a, b} and four states, S0, S1, S2 and S3, starting state S0 and one accepting state S3 On reaching the end of the input it stops and depending on which state it stopped in, it outputs a YES or a NO. S0 a b a or b S1 S2 S3 An FSA always has one or more accepting states. The states which cause the FSA to produce a YES response are called accepting states. All other states are therefore, by default, states which cause the FSA to produce a NO response. State transition diagram: a directed graph whose nodes represent the states. An edge leading from state s to state t is called a transition and is labelled with a symbolic code, e.g. a | b. The a part of the label is called the transition’s trigger and denotes the input symbol. The b part which is optional denotes the output symbol. a or b Accepting states are indicated by a state with a double ring, e.g. S3 © Dr K R Bond 2009

23 NEW A2 COMPUTING a b a or b PC Activity
S0 a b S2 S1 a or b S3 PC Activity Use the FSM simulator to implement the FSA described by this figure. Try different combinations of a’s and b’s The FSA in this figure recognizes any string that starts with an “a” and is followed by zero or more “a”s or “b”s, e.g. abbbbaaabbbaabb is a valid string, baaabbbaa is not. Check this for yourself. State transition diagram: a directed graph whose nodes represent the states. An edge leading from state s to state t is called a transition and is labelled with a symbolic code, e.g. a | b. The a part of the label is called the transition’s trigger and denotes the input symbol. The b part which is optional denotes the output symbol. © Dr K R Bond 2009


Download ppt "Finite State Machines Dr K R Bond 2009"

Similar presentations


Ads by Google