Title

ECS 120 Theory of Computation
Closure of NFA-decidable languages
David Doty (adapted from slides authored by Julian Panetta)
University of California, Davis

Recall: DFA concatenation attempt

Let’s consider naïvely attempting concatenation for these two DFAs:

data/images/dfa/parity_odd.svg data/images/dfa/one_mod3.svg

\(D_1\) \(D_2\)

If we immediately “jump” from even to q0 by redirecting the transition \(\texttt{even} \stackrel{1}{\to} \texttt{odd}\) instead to \(\texttt{even} \stackrel{1}{\to} \texttt{q0}\), then \(1111 = 111 \circ 1\) is rejected despite \(111 \in L(D_1)\) and \(1 \in L(D_2)\).

1 ??

We don’t know when to transition to q0, and even if we did, we don’t know how to avoid doing it the very first time we encounter a state with a transition to q0.

The same “where and how to split?” difficulty is encountered for Kleene star.

NFAs to the rescue

  • The ability of an NFA to “guess” makes it simpler to construct a machine deciding the concatenation and Kleene star of languages.

  • Once we demonstrate how to convert an NFA to a DFA, this will prove (indirectly) that DFA-decidable languages are closed under concatenation and Kleene star.

  • First: how can we simplify the union construction with NFAs?

    data/images/dfa/union/len_mod3_eq2/diagram.svg data/images/dfa/union/len_mod5/diagram.svg

    Create a new start state with \(\emptystring\)-transitions to the old start states.

s ε ε

Note if the two NFAs respectively have \(n\) states and \(m\) states, this has \(n + m + 1\) states, compared to \(nm\) states for the product construction.

NFAs to the rescue: Concatenation example

q0 q0 7199cb39-93c7-4201-ac0e-4df6530211cc 7199cb39-93c7-4201-ac0e-4df6530211cc->q0 q0->q0 0 q1 q1 q0->q1 1 q1->q1 0 q2 q2 q1->q2 1 q2->q0 1 q2->q2 0 e373b78d-04ad-4784-82ed-e2431916b441 even even e373b78d-04ad-4784-82ed-e2431916b441->even even->even 0 odd ->even odd odd even->odd 1 odd ->even 1 odd ->odd 0

What’s a string the new NFA accepts that it should reject?

The NFA accepts \(111\) despite \(111 \notin L(D_1) \circ L(D_2)\).

NFAs to the rescue: Concatenation example

q0 q0 q0->q0 0 q1 q1 q0->q1 1 q1->q1 0 q2 q2 q1->q2 1 q2->q0 1 q2->q2 0 e373b78d-04ad-4784-82ed-e2431916b441 even even e373b78d-04ad-4784-82ed-e2431916b441->even even->even 0 odd ->even odd odd even->odd 1 odd ->even 1 odd ->odd 0

We make all states of the left NFA rejecting, so that we only accept strings \(xy\), such that \(x\) is accepted by the left NFA and \(y\) is accepted by the right NFA.

NFAs to the rescue: Another concatenation example

d26f7849-df09-4d93-91c3-925ca4118297 q0 q0 d26f7849-df09-4d93-91c3-925ca4118297->q0 q0->q0 0 q1 q1 q0->q1 1 q1->q0 1 q2 q2 q1->q2 0 q2->q1 0 q2->q2 1 ba978f6c-6209-416b-8526-f6d2876fc697 r0 r0 ba978f6c-6209-416b-8526-f6d2876fc697->r0 r0->r0 0 r1 r1 r0->r1 1 r1->r0 1 r2 r2 r1->r2 0 r2->r1 0 r2->r2 1 d26f7849-df09-4d93-91c3-925ca4118297 q0 d26f7849-df09-4d93-91c3-925ca4118297->q0 q0->q0 0 q1 q0->q1 1 q1->q0 1 q2 q2 q1->q2 0 q2->q1 0 q2->q2 1 r0 r0 ba978f6c-6209-416b-8526-f6d2876fc697 ba978f6c-6209-416b-8526-f6d2876fc697->r0 r0->r0 0 r1 r1 r0->r1 1 r1->r0 1 r2 r2 r1->r2 0 r2->r1 0 r2->r2 1 odd ->even odd ->even

NFAs to the rescue: Kleene star example

Consider the star of \(L(N) = \setbuild{x \in \{\string{a, b}\}^*}{ x \text{ has an odd number of } \string{b} \text{'s followed by an } \string{a}}\).

data/images/nfa/odd_b_followed_a.svg
data/images/nfa/odd_b_followed_a_star_bad.svg

What’s a string the new NFA accepts that it should reject?

This accepts \(\string{bb}\) even though \(\string{bb} \notin L(N)^*\).

Since transitions enter q0, making it accepting alters the original NFA’s behavior.

NFAs to the rescue: Kleene star example

Consider the star of \(L(N) = \setbuild{x \in \{\string{a, b}\}^*}{ x \text{ has an odd number of } \string{b} \text{'s followed by an } \string{a}}\).

data/images/nfa/odd_b_followed_a.svg
data/images/nfa/odd_b_followed_a_star.svg

The new start state \(s\) can only be reached by \(\emptystring\), so making it accepting does not alter the original NFA’s behavior on any positive-length string.

Closure of NFA-decidable languages, the proofs

  • We gave example constructions showing the closure of NFA-decidable languages under:
  • Union: \(A \cup B = \setbuild{w \in \Sigma^*}{w \in A \text{ or } w \in B}\)
  • Concatenation: \(A B = \setbuild{w_1 w_2}{w_1 \in A \text{ and } w_2 \in B}\)
  • Kleene Star: \(A^* = \setbuild{w_1 w_2 \ldots w_k}{k \geq 0 \text{ and } w_i \in A}\)
  • Now we’ll give the general constructions and formally prove these closure properties.
  • We won’t prove closure under intersection or complement.
    • Intersection can be done using the product construction (no easier than for DFAs).
    • Complement is trickier than the DFA case.
    • Both of these follow immediately from the closure of DFA-decidable languages when we show equivalence of NFA- and DFA-decidability.

Closure of NFA-decidable languages under union

Theorem: the NFA-decidable languages are closed under union:
If \(A = L(N_1)\) and \(B = L(N_2)\), then we can construct \(N\) such that \(L(N) = A \cup B\).

data/images/nfa/nfa_union.svg

Proof:

  • Let \(N_1 = (Q_1, \Sigma, \Delta_1, s_1, F_1)\) and
    \(\phantom{Let }N_2 = (Q_2, \Sigma, \Delta_2, s_2, F_2)\) with \(Q_1 \cap Q_2 = \emptyset\)
  • Construct \(N = (Q, \Sigma, \Delta, s, F)\):
    • \(Q = \fragment{Q_1 \cup Q_2} \fragment{\cup \{s\}, \text{where } s \notin Q_1 \cup Q_2 \text{ is a new state}}\)
    • \(F = \fragment{F_1 \cup F_2}\)
    • For all \(q \in Q\) and \(a \in \Sigma_\emptystring\): \[ \Delta(q, a) = \begin{cases} \fragment{\Delta_1(q, a)} & \text{if } q \in Q_1 \\ \fragment{\Delta_2(q, a)} & \text{if } q \in Q_2 \\ \fragment{\{s_1, s_2\}} & \text{if } q = s \text{ and } a = \emptystring \\ \fragment{\emptyset} & \text{if } q = s \text{ and } a \in \Sigma \end{cases} \]

Alternatively: the set of transitions \(\Delta\) for \(N\) is the union \(\fragment{\Delta_1} \fragment{\cup \Delta_2} \fragment{\cup \{s \stackrel{\emptystring}{\rightarrow} s_1, s \stackrel{\emptystring}{\rightarrow} s_2 \}.}\)

Closure of NFA-decidable languages under union

Theorem: the NFA-decidable languages are closed under union:
If \(A = L(N_1)\) and \(B = L(N_2)\), then we can construct \(N\) such that \(L(N) = A \cup B\).

Proof:

data/images/nfa/nfa_union.svg
  • \(A \cup B \subseteq L(N) \quad \big(x \in A \cup B \implies x \in L(N)\big)\)
    • Suppose that \(x \in A\). Then there is a computation sequence starting at \(s_1\) (\(q_0\) in example) and ending at a state in \(F_1\).
    • We extend this to a sequence for \(N\) by prepending \(\color{red} s\). Since \(F_1 \subseteq F\) this sequence shows \(N\) accepts \(x\).
    • Thus \(x \in A \implies x \in L(N)\). Similarly \(x \in B \implies x \in L(N)\).
    • Combining both, we have \(x \in A \cup B \implies x \in L(N)\).
  • \(L(N) \subseteq A \cup B \quad \big(x \in L(N) \implies x \in A \cup B\big)\)
    • When \(x \in L(N)\) there is a computation sequence \(\color{red} \smash[t]{s \stackrel{\emptystring}{\to}} \; \color{black} r_0 \to r_1 \to \ldots \to r_n\) where either
      • \(r_0 = s_1\) and \((\forall i)\ r_i \in Q_1\) and \(r_n \in F_1\), or
      • \(r_0 = s_2\) and \((\forall i)\ r_i \in Q_2\) and \(r_n \in F_2\).
    • Then \(r_0 \to r_1 \to \ldots \to r_n\) is a computation sequence accepting \(x\) for one of \(N_1\) or \(N_2\), i.e., \(x \in A \cup B\).

Closure of NFA-decidable languages under concatenation

Theorem: the NFA-decidable languages are closed under concatenation:
If \(A = L(N_1)\) and \(B = L(N_2)\), then we can construct \(N\) such that \(L(N) = A \circ B\).

data/images/nfa/concatenation_ex2.static.svg

Proof:

  • Let \(N_1 = (Q_1, \Sigma, \Delta_1, s_1, F_1)\) and
    \(\phantom{Let }N_2 = (Q_2, \Sigma, \Delta_2, s_2, F_2)\) with \(Q_1 \cap Q_2 = \emptyset\)
  • Construct \(N = (Q, \Sigma, \Delta, s, F)\):
    • \(Q = \fragment{Q_1 \cup Q_2}\)
    • \(s = \fragment{s_1}\)
    • \(F = \fragment{F_2}\)
    • The set of transitions \(\Delta\) for \(N\) is \(\Delta_1 \cup \Delta_2 \cup \fragment{\setbuild{q \stackrel{\emptystring}{\rightarrow} s_2}{q \in F_1}}\).

Alternatively, for all \(q \in Q\) and \(a \in \Sigma_\emptystring\), \[ \Delta(q, a) = \begin{cases} \Delta_1(q, a) & \text{if } (q, a) \in Q_1 \times \Sigma_\emptystring \setminus F_1 \times \{\varepsilon\} \\ \Delta_2(q, a) & \text{if } q \in Q_2 \\ \Delta_1(q, a) \cup \{s_2\} & \text{if } q \in F_1 \text{ and } a = \emptystring \end{cases} \]

Closure of NFA-decidable languages under concatenation

Theorem: the NFA-decidable languages are closed under concatenation:
If \(A = L(N_1)\) and \(B = L(N_2)\), then we can construct \(N\) such that \(L(N) = A \circ B\).

Proof:

data/images/nfa/concatenation_ex2.static.svg
  • \(A \circ B \subseteq L(N) \ \ \big(w \in A \circ B \implies w \in L(N)\big)\)
    • Suppose that \(w \in A \circ B\).
      Then \(w = x y\) where \(x \in A\) and \(y \in B\).
    • Then there are computation sequences:
      • \(s_1, r_2, \ldots, r_n\) for \(N_1\) accepting \(x\) (with \(r_n \in F_1)\)
      • \(s_2, t_2, \ldots, t_m\) for \(N_2\) accepting \(y\) (with \(t_m \in F_2)\)
    • We can combine these into the (accepting) computation sequence for \(N\) on \(w\) \[s_1, r_2, \ldots, r_n, s_2, t_2, \ldots, t_m\] using the transition \(r_n \stackrel{\emptystring}{\to} s_2\).
    • Since \(s = s_1\) and \(t_m \in F\), this sequence proves that \(N\) accepts \(w\).

Closure of NFA-decidable languages under concatenation

Theorem: the NFA-decidable languages are closed under concatenation:
If \(A = L(N_1)\) and \(B = L(N_2)\), then we can construct \(N\) such that \(L(N) = A \circ B\).

Proof:

data/images/nfa/concatenation_ex2.static.svg
  • \(L(N) \subseteq A \circ B \ \ \big(w \in L(N) \implies w \in A \circ B\big)\)
    • Suppose \(w \in L(N)\). Then there is a computation
      sequence $s=r_0, r_1,, r_2, , r_n $, with \(r_n \in F=F_1\)
    • By construction, all paths through \(N\) reaching a
      state in \(F\) must take an \(\varepsilon\)-transition from \(r_k \in F_1\) to \(s_2\).
    • Thus, we can split the sequence into two parts: \(s_1=r_0, \ldots, r_k\) and \(s_2=r_{k + 1}, \ldots, r_n\).
      • \(r_0, \ldots, r_k\) is an accepting sequence for \(N_1\) on \(x \prefix w\) \(\fragment{\implies x \in A}\)
      • \(r_{k + 1}, \ldots, r_n\) is an accepting sequence for \(N_2\) on the remaining part \(y\) of \(w\) \(\fragment{\implies y \in B}\)
    • Thus, \(w = x y\) where \(x \in A\) and \(y \in B\), meaning \(w \in A \circ B\).

Closure of NFA-decidable languages under Kleene star

Theorem: The NFA-decidable languages are closed under Kleene star:
If \(A = L(N_1)\), then we can construct \(N\) such that \(L(N) = A^*\).

data/images/nfa/odd_b_followed_a.svg data/images/nfa/odd_b_followed_a_star.svg \(N_1\) \(N\)

Proof:

Let \(N_1 = (Q_1, \Sigma, \Delta_1, s_1, F_1)\) and construct
\(N = (Q, \Sigma, \Delta, s, F)\):

  • \(Q = Q_1 \cup \{s\}\)
  • \(F = F_1 \cup \{s\}\)
  • The set of transitions \(\Delta\) for \(N\) is
    \(\Delta_1 \cup \fragment{\{s \stackrel{\emptystring}{\rightarrow} s_1 \} \cup} \fragment{\setbuild{q \stackrel{\emptystring}{\rightarrow} s_1}{q \in F_1}}\).

Alternatively, for all \(q \in Q\) and \(a \in \Sigma_\emptystring\), \[ \Delta(q, a) = \begin{cases} \Delta_1(q, a) & \text{if } (q, a) \in Q_1 \times \Sigma_\emptystring \setminus F_1 \times \{\varepsilon\} \\ \Delta_1(q, a) \cup \{s_1\} & \text{if } q \in F_1 \text{ and } a = \emptystring \\ \{s_1\} & \text{if } q = s \text{ and } a = \emptystring \\ \emptyset & \text{if } q = s \text{ and } a \ne \emptystring \end{cases} \]

Closure of NFA-decidable languages under Kleene star

Theorem: The NFA-decidable languages are closed under Kleene star:
If \(A = L(N_1)\), then we can construct \(N\) such that \(L(N) = A^*\). (proof of \(A^* \subseteq L(N)\))

data/images/nfa/odd_b_a_star_construction.static.svg

  • Clearly \(\emptystring \in L(N)\) since \(s \in F\), so below we only consider non-empty strings.
  • To show \(A^* \subseteq L(N)\), let \(x \in A^* \setminus \{\emptystring\}\).
  • Then \(x = x_1 x_2 \dots x_k\), where each \(x_j \in L(N_1) \setminus \{\emptystring\}\).
  • For each \(1 \le j \le k\), there is a computation sequence \(q_{j,0}, q_{j,1}, \dots, q_{j,m_j} \in Q\), with \(q_{j,0} = s_1\) and \(q_{j,m_j} \in F_1\), and there is a sequence \(y_{j,0}, y_{j,1}, \dots, y_{j,m_j} \in \Sigma_\varepsilon\), where
    \(x_j = y_{j,0} y_{j,1} \dots y_{j,m_j}\), and for each \(0 \le i < m_j\), a transition \(q_{j,i} \stackrel{y_{j,i}}{\to} q_{j,i+1}\).
  • Then the following is a computation sequence showing \(N\) accepts \(x\):
    \(s, \underbrace{s_1(=q_{1,0}), q_{1,1}, \dots, q_{1,m_1}}_{x_1}, \underbrace{s_1(=q_{2,0}), q_{2,1}, \dots, q_{2,m_1}}_{x_2}, \dots, \underbrace{s_1(=q_{k,0}), q_{k,1}, \dots, q_{k,m_k}}_{x_k}\).

Closure of NFA-decidable languages under Kleene star

Theorem: The NFA-decidable languages are closed under Kleene star:
If \(A = L(N_1)\), then we can construct \(N\) such that \(L(N) = A^*\). (proof of \(L(N) \subseteq A^*\))

data/images/nfa/odd_b_a_star_construction.static.svg

  • To show \(L(N) \subseteq A^*\), let \(x \in L(N) \setminus \{\emptystring\}\). (similar to before, clearly \(\emptystring \in A^*\), so we assume \(|x|>0\))
  • Then there are sequences \(q_0,\dots,q_m \in Q'\) and \(y_0,\dots,y_{m-1} \in \Sigma_\varepsilon\) such that \(q_0=s,q_m \in F\),
    \(x = y_0 y_1 \dots y_{m-1}\), and for all \(0 \le i < m\), a transition \(q_i \stackrel{y_i}{\to} q_{i+1}\).
  • The start state \(s=q_0\) has no outgoing non-\(\varepsilon\) transitions, so the first transition \(q_0 \stackrel{y_0}{\to} q_1\) is \(s \stackrel{\varepsilon}{\to} s_1\).
    • Since there’s no transitions back to \(s\), we have \(q_m \ne s\), so \(q_m \in F_1\).
  • Suppose for \(k \ge 1\) there are \(k−1\) \(\varepsilon\)-transitions from a state in \(F_1\) to \(s_1\) in the computation sequence \(q_1,\dots,q_m\), then we can write the sequence as \(s_1,q_{1,1},\dots,q_{1,m_1},\) \(s_1,q_{2,1},\dots,q_{2,m_2},\) \(\dots,\) \(s_1,q_{k,1},\dots,q_{k,m_k},\) where each \(q_{j,m_j} \in F_1\).
  • Then each substring \(x_j\) taking \(N\) from \(s_1\) to \(q_{j,m_j}\) is in \(A \setminus \{\emptystring\}\), i.e., \(x = x_1x_2\dots x_k \in A^*\).