Interactive Notes on Finite-State Machine
· 4 min read
As I am self-studying theory of computation, specifically the theory of finite automata right now, I am making some notes on the topic to better reinforce what I have learnt, with interactive demonstrations of the automata.
A word on language
Mirroring the daily meaning of the word, in formal language theory, an alphabet is a finite1 set of characters. A string over the alphabet is a tuple in in which we write simply as . A string in has length , denoted . The set of all strings of all lengths over the alphabet is the Kleene closure of and denoted ; symbolically,
We define , where is the empty string, the only string having zero length. A language over is a subset of . As an example of a frequently-seen language, the set of all binary strings is .
Given strings and over , the concatenation of and , denoted or just , is the string produced by appending to :
Note that for any string .
type Letter = string | symbol;
type Str<L extends Letter> = Iterable<L>;
Finite-state machine
A finite-state machine (FSM)2 is specified through five elements, as follows:
where
- is the set of ’s states,
- is ’s input alphabet,
- is ’s transition function,
- is ’s initial state,
- is the set of ’s final states.
Mechanistically, an FSM can be thought of as a machine that repeatedly receives a character from an alphabet and changes its internal status accordingly. Though the above specification does not mention output, we can regard as being able to output YES or NO, depending on whether its state is one of the final states.
Naturally, we can extend the transition function to accept an arbitrary string over in an inductive manner: for and , we define
The function can receive as input any (nonempty) string over . We also let —by reading no input, the machine does not change its state. We overload to mean and assume the transition function can take arbitrary strings as input. Note that .
The FSM is said to accept a string if . In other words, the machine “output” YES when it accepts the input string and NO otherwise. The set of strings that accepts is the language accepted or recognized by .
type State = string | symbol;
class FSM<S extends State, L extends Letter> {
state: S;
initialState: S;
transition: (input: Str<L>) => void;
finalStates: Set<S>;
constructor(
initialState: S,
finalStates: Set<S>,
t: (s: S, l: L) => S
) {
this.state = initialState;
this.initialState = initialState;
this.finalStates = finalStates;
this.transition = (input: Str<L>) => {
for (const l of input) {
this.state = t(this.state, l);
}
}
}
}
FSM as labeled digraph
It is natural to visualize an FSM as a labeled directed graph: the vertices are the states and the edges are the state transitions. For a starting state and each letter , there is an edge labeled going from to .
Let us represent a simple FSM this way. The canonical example of FSM with practical use is regular expression, which are used as pattern to match or find in a string. Say, we want to know if an input string is a math equation. Assuming the string is alphanumeric and a math equation here simply contains a single ”=” sign that is not at the beginning or the end of the string. Then, we might design an FSM as follows:3
Here, the label “not =” corresponds to any character that is not the equal sign, and the label “all” matches every character. The intial state “A” has an arrow pointing to it, and the final state “E” is enclosed in a double circle.
Footnotes
-
Technically, a language can be defined on an arbitrary alphabet, but for most practical purposes, finite alphabet works without problems. ↩
-
There are many names, such as finite automaton, for the concept with various meanings. Here, “finite-state machine” follows exactly the given definition. ↩
-
For a more interactive visualization, see FSM Simulator by Ivan Zuzak. ↩