If you are enrolled in a CS program or happen to be fascinated by the aspects of theoretical Computer Science, you would most likely be introduced at some point to finite-state machines. It also might be the case that you were overwhelmed by their principles. But considering how ubiquitous this simple computational model is, it’s time to unravel its mysteries once and for all!

An everyday example

Photo by Szűcs László on Unsplash

It’s Saturday morning. You’ll wake up early today to enjoy the benefits of a morning walk, along the seaside. The clock rings at 8 am. Your eyes open, but your body feels quite relaxed under the sheets. For the next few moments, the motion of the second hand in the alarm clock is ticking rhythmically in your ears.

You finally decide to leave the bed and get ready. It’s time to go, you’re inserting the key in the front door to lock it, but the key does not match. You’re using the wrong key! It doesn’t take more than a few seconds to find the correct one and with a gentle motion, you turn the key to the right.

Photo by Brendan Greenway on Unsplash

The beach is not far from home, but quite enough to make your head quickly to the driver’s seat and start the engine. The alarm beeps reminding you to fasten the seat belt. You comply successfully with the demands of your vehicle.

Heading to your favorite spot, where the sea ​​breeze leaves a gentle, liquid sense in the air, way off the crowded area you live in, the yellow color of the traffic lights suggests slowing your speed. After two seconds, the traffic lights turn red, commanding you to stop. On your left, two people are buying from the vending machine near the coffee shop, where a couple is playing a board game. The lights turn green and you continue the ride to the desired location.

When you finally reach it, all the previous images start crossing your mind:

the alarm clock

the key hole

the car commanding you to fasten the seat belt

the traffic lights

the vending machine

the board game

You suddenly make the connection! They all represent some kind of finite-state machine.

Finite-State Machines (FSMs)

Think of a computer with extremely limited memory capacity. A device with such limited resources can seem inefficient at first glance but don’t rush to a conclusion yet. What makes a machine like this special in a world where every computer tends to be big, complicated and fancy, is the simplicity of its design.

You see, modern computers are extremely complex to allow us to create a feasible mathematical theory of them directly. Sometimes scientists need only the basics to work on a solution. For that reason, we created an idealized computer, a computational model. The simplest model is called finite-state machine (FSM).

We can visualize one using state diagrams. Look at the image below:

State diagram representing a finite-state machine. The circles represent states, where the arrows represent transitions. — Screenshot from graph IT editor.

For those who know the basics of graph theory, this is clearly a directed graph. We can see the nodes, represented by circles, and edges, represented by arrows. But unlike graphs, in state diagrams, we call them states and transitions respectively.

States represent the status of a system. For instance, automatic sliding doors in supermarkets have two states: either close or open. Traffic lights contain three states: red, yellow, green. More sophisticated examples can contain quite a lot of states, but never an infinite number. Having no states doesn’t make sense either.

When a finite-state machine is running, one or many states are active in each execution step. Those active states represent the current value of the system.

In the first execution step, a default state, called the starting state, is being activated. In state diagrams, those states are indicated by an arrow pointing to them. In the above diagram, state s is the starting state.

Finite-state machines may also contain one or multiple accepting states. Accepting states are represented by a double circle, like states p and r . A finite-state machine is not required to have accepting states; it might be designed to run indefinitely. The purpose of accepting states is quite straight forward: when the processing ends, depending on if we are on any accepting states or not, the output of the finite-state machine is either accept or reject.

States are connected with each other using transitions. Each transition contains a set of symbols. The set of all symbols presented in the FSM is called the alphabet. When the source state is active and the transition is executed, it changes the active state from the source state to the destination state only if the current input matches the symbol of the transition.

It’s like using a vending machine:

Vending machine — Photo by Stéphan Valentin on Unsplash

The active state when you first approach the machine is idle , which happens to also be the starting state. By inserting coins, you move to the next state choose product .

To recap, finite-state machines:

have a set of states and transitions

and have at least one state

state cannot have an infinite number of states

number of states have one starting state , which is the first state that is activated upon their execution

, which is the first state that is activated upon their execution may or may not have accepting states

have for each transition a set of symbols ,

, have an alphabet which is the union of all the sets of transition symbols

There are two types of finite-state machines: deterministic and non-deterministic. Based on those types, the execution is handled differently. Remember that finite-state machines are a computational model: they calculate an output based on the provided input. Below we discuss these two types and provide a complete running example for each one.

Deterministic Finite Automatons (DFAs)

The first type of finite-state machine is the Deterministic Finite Automaton (DFA). The word deterministic has quite a value here. It means that you can transition from one state to the next while not being in multiple states concurrently. Think the example with the traffic signals: they can’t be in green and red state at the same time.

Let's take a look at the bellow automaton:

Deterministic automaton with four states and five transitions. — Screenshot from graph IT editor.

The above DFA contains 4 states. Starting state is s and accepting state is r . The alphabet contains the symbols a , b , and c .

Observe that for each state, there is only one transition arrow for each symbol. In some cases, a transition arrow can contain multiple symbols, like the one from q to r , but this does not break the requirement as there is no other exiting transition from q with the same symbols ( b and c ).

In deterministic finite automatons, the transition from one state to another can happen only if the input matches the symbol(s) of the transition. For instance, we can move from s to q only if the current symbol of the input is b .

A running example

But enough with the theory! Let us see what the above DFA outputs for the input baabb.