Finite-state machines (FSMs) Dr. C. Constantinides Department of Computer Science and Software Engineering Concordia University Montreal, Canada January 10, 2017 1/19
Finite-state machines (FSMs) and state diagrams A finite-state machine (FSM), or state machine, or automaton (plural: automata), or finite-state automaton is a mathematical structure used to provide an abstract description of the behavior of a system. An FSM can model computational problems such as program design, and sequential logic circuits. 2/19
Example: An FSM to determine an even number of 1 s in a binary string Consider the finite-state machine that is used to determine whether or not a binary string contains an even number of 1 s. The machine responds to inputs. Each step in a computation is called a state. In its initial state, the machine reads the first symbol of an input string, followed by the next symbol etc. until there are no more input symbols. After each reading, the machine performs a transition to some state and possibly produces some output. 3/19
Example: An FSM to determine an even number of 1 s in a binary string /cont. The response of the machine to a given sequence of inputs is completely predictable. We say that the machine is deterministic. There is a finite number of states that the machine can attain. At any given moment the machine is in exactly one of these states. As each state depends on the previous state and the inputs, we say that the machine maintains a memory of past inputs. After having read the entire input string, the machine stops. 4/19
Example: An FSM to determine an even number of 1 s in a binary string /cont. The behavior of the FSM can be graphically represented by a state diagram: a directed graph, where the set of nodes, denoted as circles, defines the set of states (including initial and final states, where the latter is denoted as a double circle), and the set of edges defines the set of transitions. The arrow without a source node points to the initial state, S 1. 0 0 1 S 1 S 2 1 5/19
Example: An FSM to determine an even number of 1 s in a binary string /cont. Let the input string to the machine be 011. First, the machine will read 0 and it will perform a transition back to S 1. The machine will then read 1 and it will perform a transition to state S 2. Next the machine will read 1 and it will perform a transition to S 1. We see that upon having read the entire binary string, the machine is at the final state. We say that this particular input has been accepted by the FSM. 0 0 1 S 1 S 2 1 6/19
Example: An FSM to determine an even number of 1 s in a binary string /cont. The machine can also be represented by a state-transition table (or state table). The table shows what state (or states in the case of a nondeterministic finite automaton) an FSM will move to, based on the current state and other inputs. The machine begins at state s 1. If the first symbol is a 0, then the machine stays in state s 1, otherwise if the first symbol is a 1 then the next state of the machine is s 2. Current state 0 1 s 1 s 1 s 2 s 2 s 2 s 1 7/19
Example: An FSM to determine an even number of 1 s in a binary string /cont. For the input 011, we can perform an analysis of the state transition table and produce the following: Time t 0 t 1 t 2 Input 0 1 1 State s 1 s 2 s 1 8/19
Formal definition of finite-state machines Formally, a finite state machine is defined as a 5-tuple (read: quintuple ) as follows: (Q,q 0,F,Σ,δ) where 1. Q is a finite, non-empty set of states. 2. q 0 Q is the initial state (or start state). 3. F Q is a set of final states. 4. Σ is a finite, non-empty set of symbols, called the input alphabet. 5. δ is a state transition function: δ : Q Σ q Q. This function defines a deterministic finite state machine as opposed to a nondeterministic finite state machine whose state transition function returns a set of states. 9/19
Classification The following are subdivisions of finite state machines: Acceptors (also: recognizers, or sequence detectors) process all input and then produce a binary output (Yes/No) based on whether or not the input is accepted, i.e. if the current state is an accepting state. Transducers generate output based on a given input and/or a state using actions. In the Moore machine, the output depends solely on the state, whereas in the Mealy machine, the output depends on both input and state. An FSM can model the behavior of any type of entity in terms of its life-cycle and this behavior is captured by a traversal of the state diagram. 10/19
Example: An acceptor for strings ending in ab Consider a machine that can accept any binary string that ends in ab, such as ab, bab, baab, bbbaab, etc. What does it mean to execute an acceptor FSM over an input alphabet Σ? Given an FSM and a string w Power(Σ), the FSM accepts each one of the letters of w as input (from left to right) following a path starting from the start state. 11/19
Example: An acceptor for strings ending in ab /cont. Each letter causes a state transition from the start state to the next and so forth. If this path eventually ends in the final state, then we say that the FSM accepts w. Otherwise we say that the FSM rejects w. The language of an FSM is the set of all strings that it accepts. 12/19
Example: An acceptor for strings ending in ab /cont. A state transition table for the FSM of this example is shown below. Current state a b q 0 q 1 q 2 q 1 q 1 q 0 q 2 q 1 q 2 13/19
Example: An acceptor for strings ending in ab /cont. In this example, it just so happens that this is also the final state. Each bit input causes a transition either to the same state or to another state. For example, for the input string ab, we start at state q 0 and read the first bit, a, which will cause a transition to state q 1. While at state q 1 the FSM will then read b and perform a transition to state q 0. As the input string has been read and the FSM is currently at its final state, we say that the input string ab has been accepted by the FSM. 14/19
Example: An acceptor for strings ending in ab /cont. Consider an input string abb. From the initial state q 0 the FSM will read a and perform a transition to state q 1. While at state q 1 it will read b and perform a transition to state q 0. While at state q 0 it will then read b which is the final symbol in the input string and perform a transition to state q 2. As the input string has been read and the FSM is currently not at its final state, we say that the input string abb has been rejected by the FSM. 15/19
Example: An acceptor for strings ending in ab /cont. The state diagram of the FSM that can accept any binary string that ends in ab is shown below: b q 2 b a a q 0 q 1 b a 16/19
Example: An acceptor for strings ending in aab Suppose we need to build a deterministic FSM to recognize strings that end in aab such as aab, aaaab, babaab, bbabaab, aababaab, etc. 17/19
Example: An acceptor for strings ending in aab /cont. The state-transition table is shown below: Current state a b q 0 q 1 q 0 q 1 q 2 q 0 q 2 q 2 q 3 q 3 q 1 q 0 18/19
Example: An acceptor for strings ending in aab /cont. The state diagram of the FSM to recognize strings that end in aab is shown below: b a a a b q 0 q 1 q 2 q 3 b a b 19/19