Automata Theory


Automata theory is a branch of Computer Science that deals with mathematical models of computation

Finite Automaton

An abstract machine that can be in exactly one of a finite number of states at any given time. States are connected by transitions, which may be cyclic.

If AA is the set of all inputs that machine MM accepts, we say that AA is the language of machine MM and conversely that MM recognises AA. This is represented as L(M)=AL(M) = A.

Notice that a machine recognises one language and one language only. Even if the machine accepts no input, then its language is \emptyset. Two machines are equivalent if the recognise the same language.

For any set of states SS, E(S)E(S) is the set of all states that can be reached by going only through ε\varepsilon transitions, including SS. Consider the following finite automaton:

                     +-------+
             b       |       |    a, b
         +---------->|   B   |----------+
@@       |           |       |          |
@@       |           +-------+          |
 |       |                              v
 |   #########                      +-------+
 |   #       #   empty-transition   |       |
 +-->#   A   # -------------------->|   C   |
     #       #                      |       |
     #########                      +-------+
         ^                              |
         |               a              |
         +------------------------------+

In this case, E({A,B})=E({A})E({B})={A,B,C}E(\{ A, B \}) = E(\{ A \}) \cup E(\{ B \}) = \{ A, B, C \}. Notice that E({A})={A,C}E(\{ A \}) = \{ A, C \} because AA is in the argument set, and from AA we can go to CC using a ε\varepsilon transition. Similarly, E({B})={B}E(\{ B \}) = \{ B \} because there is no ε\varepsilon transition from BB.

Deterministic Finite Automaton (DFA)

A finite automaton where each state has unique transitions for any symbol of the alphabet. A DFA does not allow ε\varepsilon transitions.

It is formally defined as a 5-tuple (S,Σ,δ,s0,SA)(S, \Sigma, \delta, s_{0}, S_{A}) where SS is a finite set of states along with an error state ses_{e}, Σ\Sigma is the set denoting the alphabet, δ\delta is the transition function S×ΣSS \times \Sigma \mapsto S, s0s_{0} is the start state such that s0Ss_{0} \in S and SAS_{A} is the set of accept states such that SASS_{A} \subseteq S.

The transition function maps a state and an alphabet symbol to one next state (thus its deterministic). This function is total, and maps unspecified transitions to the ses_{e} error state.

We say that a DFA accepts a sequence of symbols w1,w2,...wnw_{1}, w_{2}, ... w_{n} if there exists a sequence of states Q=(q0,q1,...,qn)Q = (q_{0}, q_{1}, ..., q_{n}) such that q0=s0q_{0} = s_{0} (the first state is the start state), qnSAq_{n} \in S_{A} (the final state is an accept state), and riQi<nδ(ri,wi+1)=ri+1\forall r_{i} \in Q \mid i < n \bullet \delta(r_{i}, w_{i + 1}) = r_{i + 1} (each transition is valid). A machine accepts the ε\varepsilon word if its initial state is an accept state: s0SAs_{0} \in S_{A}.

Consider the following DFA that accepts binary numbers that are multiples of 3:

                    1                0
@@           +------------+    +-----------+
@@------+    |            |    |           |
        v    |            v    |           v
       ########          +------+         +------+
 +-----#      #          |      |         |      | -----+
0|     #  S1  #          |  S2  |         |  S3  |      |1
 +---->#      #          |      |         |      | <----+
       ########          +------+         +------+
             ^            |    ^           |
             |            |    |           |
             +------------+    +-----------+
                    1                0

This DFA would be formally denoted as ({s1,s2,s3,se},{0,1},δ,s1,{s1})(\{ s_{1}, s_{2}, s_{3}, s_{e} \}, \{ 0, 1 \}, \delta, s_{1}, \{ s_{1} \}), where δ\delta is {(s1,0)s1,(s1,1)s2,(s2,0)s3,(s2,1)se,(s3,0)s2,(s3,1)s3}\{ (s_{1}, 0) \mapsto s_{1}, (s_{1}, 1) \mapsto s_{2}, (s_{2}, 0) \mapsto s_{3}, (s_{2}, 1) \mapsto s_{e}, (s_{3}, 0) \mapsto s_{2}, (s_{3}, 1) \mapsto s_{3} \}.

Intersection

Given machines N1N_{1} and N2N_{2}, the intersection of both machines is the machine NN that accepts an input if both N1N_{1} or N2N_{2} do.

This is accomplished with the same process we use to convert a DFA into an NFA with the difference that the final accept states will be the cartesian product of N1N_{1} and N2N_{2} accept states instead of the members of the resulting states that contain an accept state from either N1N_{1} or N2N_{2}.

Non-deterministic Finite Automaton (NFA)

A finite automaton that allows more than one transition on the same input symbol, and that also allows ε\varepsilon transitions, which can be fulfilled by any symbol without consuming the input.

Basically, given a state and an input symbol, there can be more than one valid outbound edge for such symbol, constructing a tree. An NFA accepts a sequence of symbols if at least one of the possible paths accepts such string.

Nondeterminism is a generalization of determinism, so every DFA is automatically an NFA. Conversely, every NFA can be converted into a DFA.

An NFA is formally defined as a 5-tuple (S,Σ,δ,s0,SA)(S, \Sigma, \delta, s_{0}, S_{A}) where SS is a finite set of states, Σ\Sigma is the set denoting the alphabet, δ\delta is the transition function S×Σε(S)S \times \Sigma_{\varepsilon} \mapsto \wp (S), s0s_{0} is the start state such that s0Ss_{0} \in S and SAS_{A} is the set of accept states such that SASS_{A} \subseteq S.

Notice that this 5-tuple is similar to the one from a DFA, with the following exceptions:

Any NFA can be converted to an NFA with just one accept state by creating a new state that is the only accept state, and creating ε\varepsilon transitions from all previous accept states to the new accept state.

Union

Given machines N1N_{1} and N2N_{2}, the union of both machines is the machine NN that accepts an input if either N1N_{1} or N2N_{2} do. This is accomplished by creating a new start state that has ε\varepsilon transitions to the old start states.

@@
@@     +------+       ########
 |     |      |       #      #
 +---->|  p1  |------>#  p2  #
       |      |       #      #
       +------+       ########

@@
@@     +------+       ########
 |     |      |       #      #
 +---->|  q1  |------>#  q2  #
       |      |       #      #
       +------+       ########

                                       +------+     ########
                     empty-transition  |      |     #      #
@@                +------------------->|  p1  |---->#  p2  #
@@     +------+   |                    |      |     #      #
 |     |      |   |                    +------+     ########
 +---->|  n0  |---+
       |      |   |                    +------+     ########
       +------+   |  empty-transition  |      |     #      #
                  +------------------->|  q1  |---->#  q2  #
                                       |      |     #      #
                                       +------+     ########

Concatenation

Given machines N1N_{1} and N2N_{2}, the concatenation of both machines is the machine NN that accepts an input if N1N_{1} accepts a prefix of it, and N2N_{2} accepts the rest. This is accomplished by creating ε\varepsilon transittions between N1N_{1}’s accept states and N2N_{2} start state, making N1N_{1}’s start state the only start state, and making N2N_{2}’s accept states the only accept states.

                      ########                          ########
                      #      #                          #      #
@@                +-->#  p2  #    @@                +-->#  q2  #
@@     +------+   |   #      #    @@     +------+   |   #      #
 |     |      |   |   ########     |     |      |   |   ########
 +---->|  p1  |---+                +---->|  q1  |---+
       |      |   |   ########           |      |   |   ########
       +------+   |   #      #           +------+   |   #      #
                  +-->#  p3  #                      +-->#  q3  #
                      #      #                          #      #
                      ########                          ########



                      ########                                         ########
                      #      #  empty-transition                       #      #
@@                +-->#  p2  #--------------------+                +-->#  q2  #
@@     +------+   |   #      #                    |     +------+   |   #      #
 |     |      |   |   ########                    |     |      |   |   ########
 +---->|  p1  |---+                               +---->|  q1  |---+
       |      |   |   ########                    |     |      |   |   ########
       +------+   |   #      #  empty-transition  |     +------+   |   #      #
                  +-->#  p3  #--------------------+                +-->#  q3  #
                      #      #                                         #      #
                      ########                                         ########

Kleene Star

Given machine NN, N*N^{*} is the machine that accepts zero or more iterations of NN. This is accomplished by creating a new start state that is an accept state (so that the ε\varepsilon word is accepted, as it is always a member of the star set), and has a ε\varepsilon transition to the old start state, and creating ε\varepsilon transitions from previous accept states back to the old start state.

                      ########
                      #      #
@@                +-->#  p2  #
@@     +------+   |   #      #
 |     |      |   |   ########
 +---->|  p1  |---+
       |      |   |   ########
       +------+   |   #      #
                  +-->#  p3  #
                      #      #
                      ########
                                        empty-transition
                                     +--------------------+
                                     |                    |
                                     |          ########  |
                                     |          #      #  |
@@                                   v      +-->#  p2  #--+
@@     ########                  +------+   |   #      #
 |     #      # empty-transition |      |   |   ########
 +---->#  s0  #----------------->|  p1  |---+
       #      #                  |      |   |   ########
       ########                  +------+   |   #      #
                                     ^      +-->#  p3  #--+
                                     |          #      #  |
                                     |          ########  |
                                     |                    |
                                     +--------------------+
                                         empty-transition

Generalised Non-deterministic Finite Automaton (GNFA)

A GNFA is a NFA where the transitions can be regular expressions instead of just words from an alphabet, or ε\varepsilon. Therefore, a GNFA reads blocks of symbols from the input, and not necessarily just one symbol at a time like other NFAs.

A GNFA is formally defined as a 5-tuple (S,Σ,δ,s0,SA)(S, \Sigma, \delta, s_{0}, S_{A}) where SS is a finite set of states, Σ\Sigma is the set denoting the alphabet, δ\delta is the transition function S×SS \times S \mapsto \mathcal{R}, where \mathcal{R} is the set of all regular expressions over Σ\Sigma, s0s_{0} is the start state such that s0Ss_{0} \in S and SAS_{A} is the set of accept states such that SASS_{A} \subseteq S.

Notice that given siSs_{i} \in S and sjSs_{j} \in S, δ(si,sj)=Rk\delta (s_{i}, s_{j}) = R_{k} means that the transition between sis_{i} and sjs_{j} is the regular expression RkR_{k}.

For convenience when converting GNFAs to regular expressions, GNFAs always have a special form with the following conditions:

Notice that we if assume this special form, then the domain of the transition function is (S\SA)×(S\{s0})(S \setminus S_{A}) \times (S \setminus \{ s_{0} \}). This is because we don’t allow transitions from the accept state to other states, nor transitions going to the start state.

Every GNFA is equivalent to a GNFA with only two states with a transition that includes a regular expression that abstracts away any other states and transitions.

For example, given Σ={a,b}\Sigma = \{ a, b \}:

           +---------------------------------------+
           |                  b                    |
           |                                       v
           |                                   ########
           |                  ab U ba          #      #
           |           +---------------------->#  s3  #
           |           |                       #      #
       +------+        |                       ########
@@     |      |        |   empty-set               ^
@@---->|  s0  |--------+------------------+        |
       |      |        |                  |        |
       +------+        |                  v        |b*
           |       +------+           +------+     |
           | ab*   |      |     a*    |      |     |
           +------>|  s1  |---------->|  s2  |-----+
             +---->|      |           |      |<---+
             |     +------+           +------+    |
             | aa  |   ^                  | |   ab|
             +-----+   |      (aa)*       | +-----+
                       +------------------+

A GNFA accepts a word wΣ*w \in \Sigma^{*} if it can be partitioned into a set of words w1w2...wiw_{1} w_{2} ... w_{i} where each partition is a member of Σ*\Sigma^{*} and there exists a sequence of states s0s1...sjs_{0} s_{1} ... s_{j} such that s1s_{1} is the start state, sjs_{j} is an accept state (the only one in the case of a GNFA in the special form), and that δ(si1,si)=Ri\delta (s_{i - 1}, s_{i}) = R_{i} where w1w_{1} is a member of the language such regular expression represents: wiL(Ri)w_{i} \in L(R_{i}).

Basically, there is a sequence of states and regular expression transitions that consumes the input in chunks, in order, in a way that such computation ends in an accept state.

Finite State Transducer (FST)

A type of DFA where the output is a word rather than a boolean accept or reject result. This DFA has no accept states, and deals with two different alphabets. A FST translates a word from an alphabet to another alphabet.

A FST is formally defined as a 5-tuple (S,Σ,Γ,δ,s0)(S, \Sigma, \Gamma, \delta, s_{0}) where SS is a finite set of states, Σ\Sigma is the input alphabet, Γ\Gamma is the output alphabet, δ\delta is the transition function S×ΣS×ΓS \times \Sigma \mapsto S \times \Gamma, and s0s_{0} is the start state such that s0Ss_{0} \in S.

Basically, the transition function takes a state and a symbol from the input alphabet, and returns the next state along with the translated symbol from the output alphabet.

For example, given Σ={a,b}\Sigma = \{ a, b \} and Γ={0,1}\Gamma = \{ 0, 1 \}:

                          b/1
           +-------------------------------+
           |                               |
           +---------------+               |
           |      a/1      |               |
@@         |               v               v
@@     +------+        +------+        +------+
 |     |      |        |      |  a/1   |      |
 +---->|  s1  |        |  s2  |------->|  s3  |
       |      |        |      |        |      |
       +------+        +------+        +------+
           ^             |  ^              |
           |      b/0    |  |     b/1      |
           +-------------+  +--------------+
           |                               |
           +-------------------------------+
                          a/0

The transition notation x/yx / y between sis_{i} and sjs_{j} means that moving between those state takes xx from the input alphabet, and will result in yy from the output alphabet. Given the above example, we can translate aabb to 1110. The transition function is defined as something like this: δ(s1,a)=(s2,1)\delta (s_{1}, a) = (s_{2}, 1).

Converting an NFA into a DFA

Given an NFA with states SS, then its corresponding DFA will have (S)\wp (S) as states (notice it always includes \emptyset). Because the DFA states are the power set of the NFA sets, then given an NFA with kk states, then its DFA will have 2k2^{k} states. Notice that we don’t need the error state ses_{e} in this case since the \emptyset state will serve that purpose.

The DFA alphabet is the same as in the NFA, leaving aside the ε\varepsilon symbol.

In order to calculate the new transition function, we must go through each of the sets in (S)\wp (S), and for each of those, go through the possible alphabet symbols (excluding ε\varepsilon), and return the union of all the valid transitions from each of the states. We can take ε\varepsilon transitions and return the resulting states as well, but only after consuming the input symbol.

Given the start state of the NFA is s0s_{0}, the start state of the DFA is equal to E({s0})E(\{ s_{0} \}).

The DFA accept states are all the members of (S)\wp (S) that contain at least one of the NFA accept states.

Finally, we can try to simplify the DFA by discarding states that only have outbound transitions, which means no path can lead into them.

Consider the following NFA:

                        a
                      +---+
                      |   |
                      |   v
                     +-----+
             b       |     |    a, b
          +--------->|  B  |---------+
          |          |     |         |
@@        |          +-----+         v
@@     #######                    +-----+
 |     #     #  empty-transition  |     |
 +---->#  A  #------------------->|  C  |
       #     #                    |     |
       #######                    +-----+
          ^                          |
          |           a              |
          +--------------------------+

If we build its corresponding DFA, then its states are ({A,B,C})={,{A},{B},{C},{A,B},{A,C},{B,C},{A,B,C}}\wp (\{ A, B, C\}) = \{ \emptyset, \{A\}, \{B\}, \{C\}, \{A, B\}, \{A, C\}, \{B, C\}, \{A, B, C\}\}, the alphabet is {a,b}\{ a, b \}, the start state is E({A})={A,C}E(\{ A \}) = \{ A, C \}, and the accept states are {{A},{A,B},{A,C},{A,B,C}}\{ \{A\}, \{A, B\}, \{A, C\}, \{A, B, C\}\}.

The transition function looks like this:

a b
δ(,a)=\delta (\emptyset, a) = \emptyset δ(,b)=\delta (\emptyset, b) = \emptyset
δ({A},a)=\delta (\{ A \}, a) = \emptyset δ({A},b)={B}\delta (\{ A \}, b) = \{ B \}
δ({B},a)={B,C}\delta (\{ B \}, a) = \{ B, C \} δ({B},b)={C}\delta (\{ B \}, b) = \{ C \}
δ({C},a)={A,C}\delta (\{ C \}, a) = \{ A, C \} δ({C},b)=\delta (\{ C \}, b) = \emptyset
δ({A,B},a)={B,C}\delta (\{ A, B \}, a) = \{ B, C\} δ({A,B},b)={B,C}\delta (\{ A, B \}, b) = \{ B, C \}
δ({A,C},a)={A,C}\delta (\{ A, C \}, a) = \{ A, C \} δ({A,C},b)={B}\delta (\{ A, C \}, b) = \{ B \}
δ({B,C},a)={A,B,C}\delta (\{ B, C \}, a) = \{ A, B, C \} δ({B,C},b)={C}\delta (\{ B, C \}, b) = \{ C \}
δ({A,B,C},a)={A,B,C}\delta (\{ A, B, C \}, a) = \{ A, B, C \} δ({A,B,C},b)={B,C}\delta (\{ A, B, C \}, b) = \{ B, C \}

Notice δ({C},a)={A,C}\delta (\{ C \}, a) = \{ A, C \}. From CC, we can take the aa transition to AA, and from there we can take ε\varepsilon back to CC, so we count CC as well.

Also notice that δ({A},a)=\delta (\{ A \}, a) = \emptyset. You might expect that we can follow ε\varepsilon to CC, and from there take aa back to AA, but ε\varepsilon transitions can only happen after the input symbol was consumed.

The resulting DFA looks like this:

          +--------------------------------------------------------+
          |                           a                            |
          |                                                        |
       #######           #########                                 |
       #     #           #       #               +--------+        |
       # {A} #           # {A,B} #-------+       | a      |        |
       #     #           #       #     a |       |        v        |
       #######           #########       v       |    #########    |
          |                  |       +-------+   |    #       #    |
          +------b-----+     | b     |       |   | +--#{A,B,C}#    |
                       |     +------>| {B,C} |---+ |  #       #    |
                       v             |       |     |  #########    |
                    +-----+          +-------+     |   |     ^     |
                    |     |              | ^       |   |  a  |     |
          +-------->| {B} |---------+    | |       |   +-----+     |
          |  b      |     |     b   |    | +-------+               |
          |         +-----+         |    |     b         +------+  |
      #########                     |    |b              |      |  |
@@    #       #                     v    |      +------->|  {}  |<-+
@@---># {A,C} #<-------+         +-----+ |      |   +----|      |----+
      #       #        | a       |     | |      |   |    +------+    |
      #########        +---------| {C} |<+      |   |      ^  ^      |
       |     ^                   |     |        |   |  a   |  |   b  |
       |  a  |                   +-----+        |   +------+  +------+
       +-----+                      |           |
                                    |        b  |
                                    +-----------+

Removing states with only outbound transitions results in:

                                                 +--------+
                                                 | a      |
                                                 |        v
                                                 |    #########
                                     +-------+   |    #       #
                             a       |       |   | +--#{A,B,C}#
                       +------------>| {B,C} |---+ |  #       #
                       |             |       |     |  #########
                    +-----+          +-------+     |   |     ^
                    |     |              | ^       |   |  a  |
          +-------->| {B} |---------+    | |       |   +-----+
          |  b      |     |     b   |    | +-------+
          |         +-----+         |    |     b         +------+
      #########                     |    |b              |      |
@@    #       #                     v    |      +------->|  {}  |
@@---># {A,C} #<-------+         +-----+ |      |   +----|      |----+
      #       #        | a       |     | |      |   |    +------+    |
      #########        +---------| {C} |<+      |   |      ^  ^      |
       |     ^                   |     |        |   |  a   |  |   b  |
       |  a  |                   +-----+        |   +------+  +------+
       +-----+                      |           |
                                    |        b  |
                                    +-----------+

Converting an DFA into a GNFA

Add a new start state with an ε\varepsilon transition to the old start state, then add one single new accept state and create ε\varepsilon transitions to it from all old accept states. At this point, if any transitions have multiple labels or there is more than one transition in the same directory between two states, replace them with a single transition whose label is the union of the previous labels. Finally, add \emptyset transitions between all states (except the start and final state).

Converting a GNFA into a Regular Expression

This conversion algorithm assumes the GNFA is the special form. A GNFA in this form has k2k \geq 2 states, since it always has a start and acceptt state that are ensured to be different. The idea is that while k>2k > 2, we can pick any state that is not the start or the accept state, remove it, and “fix” the broken transitions by abstracting the removed computation as regular expressions. Once k=2k = 2, the regular expression in the transition between the start and the final state is the result.

Consider the following automata:

+------+                +------+
|      |       R1       |      |
|  s1  |--------------->|  s2  |
|      |                |      |
+------+                +------+
    |       +------+        ^
    | R2    |      |     R3 |
    +------>|  s3  |--------+
            |      |
            +------+
             |    ^
             | R4 |
             +----+

We will remove s3s_{3}. The computation between s1s_{1} and s2s_{2} that went through s3s_{3} can be summarized as: R2R_{2}, then zero or more R4R_{4}, and finally R3R_{3}. In regular expression terms, this would be R2R4*R3R_{2} \circ R_{4}^{*} \circ R_{3}, so:

+------+                +------+
|      |       R1       |      |
|  s1  |--------------->|  s2  |
|      |                |      |
+------+                +------+
    |                       ^
    |       R2 R4* R3       |
    +-----------------------+

Now we have two transitions from the same states, which we can collapse using the union operator: (R2R4*R3)R1(R_{2} \circ R_{4}^{*} \circ R_{3}) \cup R_{1}.

Notice that because of the pumping lemma, we can represent any regular language as xy*zxy^{*}z, and therefore any sub-tree of a finite automata. Thus, the removal of any state can be resolved with the concatenation of, the transition going to the removed state (xx), is the transition going from the removed state to itself (y*y^{*}), and the transition going from the removed state to the next state. Finally, we calculate the union of this resulting expression and any other transition between the state that goes to the removed state to the state after the removed state.

Formally, assume (S,Σ,δ,s0,SA)(S, \Sigma, \delta, s_{0}, S_{A}) and sripSs_{rip} \in S where sripSAs_{rip} \notin S_{A} and srips0s_{rip} \neq s_{0}. The resulting GNFA with srips_{rip} is (S,Σ,δ,s0,SA)(S', \Sigma, \delta', s_{0}, S_{A}), where S=S\{srip}S' = S \setminus \{ s_{rip} \} and for any siS\SAs_{i} \in S' \setminus S_{A} and any sjS\{s0}s_{j} \in S' \setminus \{ s_{0}\}, δ(si,sj)=(R1R2*R3)R4\delta'(s_{i}, s_{j}) = (R_{1} R_{2}^{*} R_{3}) \cup R_{4} where R1=δ(si,srip)R_{1} = \delta(s_{i}, s_{rip}), R2=δ(srip,srip)R_{2} = \delta(s_{rip}, s_{rip}), R3=δ(srip,sj)R_{3} = \delta (s_{rip}, s_{j}), and R4=δ(si,sj)R_{4} = \delta(s_{i}, s_{j}).

Converting a Regular Expression into an NFA

Consider the regular expression RR. We will use the 6-clause definition of a regular expression, and discuss them in order.

If R=aR = a where aΣa \in \Sigma, then the language of the regular expression is the set containing just the aa symbol: L(R)={a}L(R) = \{ a \}. The resulting NFA is then ({s0,s1},Σ,δ,s0,{s1})(\{ s_{0}, s_{1} \}, \Sigma, \delta, s_{0}, \{ s_{1} \}) where δ(s0,a)=s1\delta(s_{0}, a) = s_{1} and xSigmaxaδ(s0,x)=\forall x \in Sigma \mid x \neq a \bullet \delta(s_{0}, x) = \emptyset:

        +------+      ########
@@      |      |  a   #      #
@@----->|  s0  |----->#  s1  #
        |      |      #      #
        +------+      ########

If R=εR = \varepsilon, then L(R)={ε}L(R) = \{ \varepsilon \}, so the resulting NFA is ({s0},Σ,δ,s0,{s0})(\{s_{0}\}, \Sigma, \delta, s_{0}, \{s_{0}\}) where xΣδ(s0,x)=\forall x \in \Sigma \bullet \delta(s_{0}, x) = \emptyset:

         ########
@@       #      #
@@-----> #  s0  #
         #      #
         ########

If R=R = \emptyset, then L(R)=L(R) = \emptyset, so the resulting NFA is ({s0},Σ,δ,s0,)(\{s_{0}\}, \Sigma, \delta, s_{0}, \emptyset) where xΣδ(s0,x)=\forall x \in \Sigma \bullet \delta(s_{0}, x) = \emptyset:

         +------+
@@       |      |
@@-----> |  s0  |
         |      |
         +------+

For the last 3 cases, where RR is the union or concatenation between other regular expressions, and where RR is the Kleene star of a regular expression, we convert each of the operands to NFAs using the definitions described before, and then use the standard way to represent union, concatenation, or stars of NFAs (consult the Operations section).

Consider (aba)*(ab \cup a)^{*} as a complete example. First, lets convert aa and bb into NFAs:

        +------+      ########
@@      |      |  a   #      #
@@----->|  s0  |----->#  s1  #
        |      |      #      #
        +------+      ########

        +------+      ########
@@      |      |  b   #      #
@@----->|  s2  |----->#  s3  #
        |      |      #      #
        +------+      ########

The expression abab is the concatenation of both previous expressions, which is:

        +------+      +------+                  +------+      ########
@@      |      |  a   |      | empty-transition |      |  b   #      #
@@----->|  s0  |----->|  s1  |----------------->|  s2  |----->#  s3  #
        |      |      |      |                  |      |      #      #
        +------+      +------+                  +------+      ########

The expression abaab \cup a is the union of the previous NFA and aa’s NFA:

                      +------+      +------+
     empty-transition |      |  a   |      | empty-transition
         +----------->|  s1  |----->|  s2  |------+
         |            |      |      |      |      |      +------+      ########
     +------+         +------+      +------+      |      |      |  b   #      #
@@   |      |                                     +----->|  s3  |----->#  s4  #
@@-->|  s0  |                                            |      |      #      #
     |      |                                            +------+      ########
     +------+         +------+      ########
         |            |      |  a   #      #
         +----------->|  s5  |----->#  s6  #
     empty-transition |      |      #      #
                      +------+      ########

Finally, we calculate the Kleene star of the whole previous NFA:

                                    +------+       +------+
                   empty-transition |      |   a   |      |  empty-transition
                              +---->|  s2  |------>|  s3  |------+
                              |     |      |       |      |      |      +------+
 @@                           |     +------+       +------+      |      |      |
 @@-+                         |                                  +----->|  s4  |
    v                         |                                         |      |
+------+                  +------+                +------+     ######## +------+
|      | empty-transition |      |empty-transition|      |  a  #      #     |
|  s0  |----------------->|  s1  |--------------->|  s6  |---->#  s7  #     |  b
|      |                  |      |                |      |     #      #     v
+------+                  +------+                +------+     ######## ########
                              ^                                    |    #      #
                              |                                    |    #  s5  #
                              +------------------------------------+    #      #
                              |          empty-transition               ########
                              |                                             |
                              |                                             |
                              +---------------------------------------------+
                                                empty-transition

Converting a DFA into a Regular Expression

We have to first convert the DFA into a GNFA, and then convert the GNFA into a regular expression, using the mechanisms described before.

Regular Expressions

RR is a regular expression if RR is the language containing one symbol ss from an alphabet Σ\Sigma, R={ε}R = \{ \varepsilon \}, R=R = \emptyset, the union or concatenation between two regular expressions, or the Kleene star of a regular expression.

Notice the regular expression ε\varepsilon denotes the language containing just the empty string, while \emptyset denotes the language that does not contain any string.

Any regular expression can be converted into a finite automaton that recognizes the language it describes, and vice versa. If a language can be described by a regular expression, then it is a regular language.

Words

A word is a member of language, and consists of symbols that are part of an alphabet.

Cardinality

Given a word ss from a language AA, the “length” of ss is denoted as |s|| s |. Given Σ={0,1}\Sigma = \{ 0, 1 \} and s=001100s = 001100, |s|=6| s | = 6.

Reverse

Given a word w=w1w2...wnw = w_{1}w_{2} ... w_{n}, the reverse of the word is ww^{\mathcal{R}} equals wn...w2,w1w_{n} ... w_{2}, w_{1}.

Regular Languages

A language is called a regular language if and only if there exists a non-deterministic finite automaton that recognises it.

Union

Given two regular languages AA and BB, the union of both languages is represented as AB={xxAxB}A \cup B = \{ x \mid x \in A \lor x \in B \}, and it gives us a language containing all the words for both AA and BB. The union of two regular languages is a regular language.

For example, given A={foo,bar}A = \{ foo, bar\} and B={baz,qux}B = \{ baz, qux\}, then AB={foo,bar,baz,qux}A \cup B = \{ foo, bar, baz, qux \}.

For convenience, given words s1s_{1} and s2s_{2}, s1s2s_{1} \cup s_{2} is a shorthand for {s1}{s2}\{s_{1}\} \cup \{s_{2}\}.

Notice A=AA \cup \emptyset = A, but AεA \cup \varepsilon might not always equal AA.

Concatenation

Given two regular languages AA and BB, the concatenation of both languages is represented as AB={xyxAxB}A \circ B = \{ xy \mid x \in A \land x \in B \}, and it gives us a language containing all possible combinations of words in AA before words in BB, and viceversa, like a cartesian product. The concatenation of two regular languages is a regular language.

For example, given A={foo,bar}A = \{ foo, bar\} and B={baz,qux}B = \{ baz, qux\}, then AB={foobaz,fooqux,barbaz,barqux}A \circ B = \{ foobaz, fooqux, barbaz, barqux \}.

For convenience, given words s1s_{1} and s2s_{2}, both s1s2s_{1} \circ s_{2} and s1s2s_{1} s_{2} are shorthands for {s1}{s2}\{s_{1}\} \circ \{s_{2}\}.

Concatenating any set to the empty set yields the empty set: x=x \circ \emptyset = \emptyset. Also Aε=AA \circ \varepsilon = A, but AA \circ \emptyset might not always equal AA.

Kleene Star

This is a unary operation that given a language AA, equals an infinite set of all the possible combinations of words in AA. It is defined as A*={x1x2...xkk0xiA}A^{*} = \{x_{1}x_{2} ... x_{k} \mid k \geq 0 \land x_{i} \in A\}. For any language, ε\varepsilon is a member of the star result. The Kleene star of a regular language is a regular language. Notice that *={ε}\emptyset^{*} = \{ \varepsilon \}.

For example, given A={foo,bar}A = \{ foo, bar\}, A*={ε,foo,bar,foofoo,foobar,barbar,barfoo,foofoofoo,foofoobar,...}A^{*} = \{ \varepsilon, foo, bar, foofoo, foobar, barbar, barfoo, foofoofoo, foofoobar, ... \}.

For convenience, given a word ss, s*s^{*} is a shorthand for {s}*\{s\}^{*}.

Given alphabet Σ\Sigma, Σ*\Sigma^{*} is the language consisting of all strings over such alphabet.

As an extension to the Kleene star operator, we can define s+=ss*s^{+} = s \circ s^{*}. Also, s*=s+εs^{*} = s^{+} \cup \varepsilon. Notice that given a language AA, A=A+AAAA = A^{+} \iff A \circ A \subseteq A.

Notice that *={ε}\emptyset^{*} = \{ \varepsilon \}. This means “nothing” zero or more times. The zero part of it comes down to ε\varepsilon. Nothing two times is \emptyset \circ \emptyset, which is \emptyset.

Reverse

The reverse of a language is the language with all its words reserved. Given AA, the reverse version of the language is A={wwA}A^{\mathcal{R}} = \{ w^{\mathcal{R}} \mid w \in A \}. If AA is regular, so is AA^{\mathcal{R}}.

References