Decompiling a regular expression from an NFA [017Z]
- February 14, 2025
- February 14, 2025
- Jon Sterling
We want to “decompile” any NFA to an equivalent regular expression; it is not easy to do so directly, so we will actually solve a more general problem first.
Definition Region of an NFA [0181]
- February 14, 2025
- Jon Sterling
Let M be an NFA. A region of M is defined to be a triple {\mathopen {}\left (q,S,q'\right )\mathclose {}} with S\subseteq Q_{M} and q,q'\in Q_{M}.
A region {\mathopen {}\left (q,S,q'\right )\mathclose {}} in an NFA M is meant to encode a language whose strings consist of those that can be consumed starting from states q and ending in state q' passing only through states that lie in S. We define this more formally below.
Definition The formal language accepted within a region [0182]
- February 14, 2025
- Jon Sterling
Let {\mathopen {}\left (q,S,q'\right )\mathclose {}} be a region in an NFA M. The formal language accepted within {\mathopen {}\left (q,S,q'\right )\mathclose {}} is defined to be the following set of strings:
\mathcal {L}_{M}{\mathopen {}\left (q,S,q'\right )\mathclose {}} = {\mathopen {}\left \{u :\Sigma _M^\star \,\middle \vert \, q \mathrel {\vphantom {\xrightarrow {u}}\smash {\xrightarrow {u}}^*} q' \text { with all intermediate states lying in S} \right \}\mathclose {}}We shall also refer to \mathcal {L}_{M}{\mathopen {}\left (q,S,q'\right )\mathclose {}} as the regional language of M in {\mathopen {}\left (q,S,q'\right )\mathclose {}}.
Lemma Generalised NFA decompilation [0180]
- February 14, 2025
- Jon Sterling
Any regional language of an NFA is regular.
Proof.
- February 14, 2025
- Jon Sterling
We proceed by induction on the number of elements in S.
In the base case, we know that S is empty; the regional language \mathcal {L}_{M}{\mathopen {}\left (q,\varnothing ,q'\right )\mathclose {}} therefore consist of those strings u such that either u=\epsilon and q=q', or u=a for some a:\Sigma _M and q\mathrel {\xrightarrow {a}}q'. Writing this more formally, we have:
\mathcal {L}_{M}{\mathopen {}\left (q,\varnothing ,q'\right )\mathclose {}} = {\mathopen {}\left \{\epsilon \,\middle \vert \,q=q'\right \}\mathclose {}}\cup \bigcup {\mathopen {}\left \{{\mathopen {}\left \{a\right \}\mathclose {}}\,\middle \vert \,q\mathrel {\xrightarrow {a}}q'\right \}\mathclose {}}The above is a finite union of regular languages, and so it is evidently regular.
In the inductive step, we assume that any regional language whose interior state set has n elements is regular, and we must show the same for any regional language whose interior state set has n+1 elements. Fixing such a region {\mathopen {}\left (q,S,q'\right )\mathclose {}} with \#S=n+1, we will try to reduce the \mathcal {L}_{M}{\mathopen {}\left (q,S,q'\right )\mathclose {}} to something in terms languages that we know by our inductive hypothesis to be regular. Our strategy is to simply remove an arbitrary element from S.
To that end, we pick some arbitrary q_0\in S so that the set difference S' = S\setminus {\mathopen {}\left \{q_0\right \}\mathclose {}} has n elements. IF we think about all the ways taht we could get from q to q' passing through states lying only in S, there are a two possibilities in relation to the state q_0:
- We might have never pass through q_0 at all. This would correspond to the regional language \mathcal {L}_{M}{\mathopen {}\left (q,S',q'\right )\mathclose {}}, which we know to be regular by our induction hypothesis.
- If we do pass through q_0, we must first go from q to q_0 without passing through q_0, then we return to q_0 arbitrarily many times, and then we finally go from q_0 to q without passing through q_0. This corresponds to the following concatenation of languages, which we know to be regular by our induction hypothesis and the fact that regular languages are closed under concatenation and repetition: \mathcal {L}_{M}{\mathopen {}\left (q,S',q_0\right )\mathclose {}} \mathcal {L}_{M}{\mathopen {}\left (q_0,S',q_0\right )\mathclose {}}^\star \mathcal {L}_{M}{\mathopen {}\left (q_0,S',q'\right )\mathclose {}}
We see the following by inspection:
\mathcal {L}_{M}{\mathopen {}\left (q,S,q'\right )\mathclose {}} = \mathcal {L}_{M}{\mathopen {}\left (q,S',q\right )\mathclose {}} \cup {\mathopen {}\left ( \mathcal {L}_{M}{\mathopen {}\left (q,S',q_0\right )\mathclose {}} \mathcal {L}_{M}{\mathopen {}\left (q_0,S',q_0\right )\mathclose {}}^\star \mathcal {L}_{M}{\mathopen {}\left (q_0,S',q'\right )\mathclose {}} \right )\mathclose {}}As regular languages are closed under finite unions, we are done.
Corollary Decompilation of NFAs [0183]
- February 14, 2025
- Jon Sterling
Any automatical language is regular.
Proof.
- February 14, 2025
- Jon Sterling
We have shown in Lemma [0180] that each regional langauge of an NFA is regular; regular languages are closed under finite unions, so it suffices to decompose \mathcal {L}{\mathopen {}\left (M\right )\mathclose {}} into a finite union of regional languages \mathcal {L}_{M}{\mathopen {}\left (q,S,q'\right )\mathclose {}}.
We note that if a string is accepted by M, it does so by means of a path from the starting state to some accepting state; each of these scenarios for a given accepting state specifies a region {\mathopen {}\left (s_{M},Q_{M},q\right )\mathclose {}} with q\in F_{M}. Therefore, we immediately deduce:
\mathcal {L}{\mathopen {}\left (M\right )\mathclose {}} = \bigcup _{q\in F_{M}} \mathcal {L}_{M}{\mathopen {}\left (s_{M},Q_{M},q\right )\mathclose {}}Therefore it follows that \mathcal {L}{\mathopen {}\left (M\right )\mathclose {}} is regular.