✖

Converting an automaton to a regular expression

Let’s see how to convert any finite automaton to a regular expression.

The algorithm

The input is some NFA A. First we turn it into a GNFA. In the second phase, in each step we remove one state, adjust the transition function accordingly and repeat until only two states remain: the start state and the accept state. There is an edge between them whose label is a regular expression, and this is the result of the whole algorithm. So the whole procedure consists mainly of removing a state and then repairing the automaton so that we obtain an equivalent one.

Removing one state

We can illustrate how to remove one state with a simple example. Suppose part of our automaton looks like this:

where Ri are some regular expressions. What would the automaton look like if we removed state qr? We can think of it like this: we are in state qi and ask which strings we can generate along this stretch. If we go from state qi straight to state qj, these are all the strings that match the regular expression R4.

If we go to state qr instead, we can generate strings of the form R1. But in state qr we can loop on the regular expression R2, so we can in fact generate strings of the form R1∘(R2∗). And since we can still get from state qr to state qj, we add the regular expression R3 at the end. Altogether, this route gives us strings of the form R1∘(R2∗)∘R3.

Now we know that this part of the automaton can produce strings of the form R4 or of the form R1∘(R2∗)∘R3. We can of course write this as a regular expression: (R4)|(R1∘(R2∗)∘R3).

Now we can simply remove state qr, leaving just the two states qi and qj, and in place of R4 we write the expression we have just worked out:

We carry out this procedure for every edge from every state qi to some state qj, including loops, that is, including the case where qi = qj.

After removing one state we get an equivalent automaton, that is, an automaton that recognises the same language.

The complete algorithm

The input is an NFA A = ⟨Q, Σ, δ, q0, F⟩.

  1. Convert the NFA A to a GNFA Z = ⟨Q′, Σ, δ′, q0′, qf′⟩. From now on we write k for the number of states in Z.
  2. If k = 2, the algorithm ends and the edge between the start state and the accept state carries the resulting regular expression.
  3. If k>2, choose any state qr that differs from the start and accept states, that is, qr ≠ q0′ and qr ≠ qf′. Then create a new GNFA Z′ = ⟨Q′′, Σ, δ′′,q0′,qf′⟩ for which the following holds: Q′′=Q′∖{qr} and for all qi ∈ Q′′ ∖ {qf′} and all qj ∈ Q′′ ∖ {q0′} let δ′′(qi,qj)=(R4)|(R1(R2∗)R3), where R1 = δ′(qi,qr), R2 = δ′(qr,qr), R3 = δ′(qr, qj), R4 = δ′(qi, qj). Then continue with step 2.

Example

Suppose the input is this finite automaton:

First we convert it to a GNFA (we won’t draw the unnecessary ∅-transitions):

Now we apply the second part of the algorithm and remove a node. We start with node q2. We remove node q2 and add a transition from state q1 to state qf. We label this transition b(a|b)∗, because from node q1 we get to state q2 on strings of the form b, then we can loop on a|b, which gives us (a|b)∗, and finally we move to qf using the epsilon rule. Note that b(a|b)∗ϵ = b(a|b)∗. We get the automaton:

We still have to remove the last state, q1. We add an edge from state qs to state qf. How do we label it? We get to state q1 through an epsilon rule, which we can simply drop. Then we can loop on a, so we obtain the regular expression a∗. And finally we follow the edge to qf, so we concatenate the expression with b(a|b)∗. We therefore label the edge with the regular expression a∗ b(a|b)∗.

The automaton now has only two states, so the algorithm ends. The edge carries the resulting regular expression.

Sources

Chapters: Regular expressions, Converting a regular expression to an automaton, Generalised non-deterministic finite automaton, Converting an automaton to a regular expression