Title

ECS 120 Theory of Computation
Equivalence of computational models
Julian Panetta
University of California, Davis

Equivalence of computational models

  • We’ve shown how to prove closure of a class of languages decidable by a certain model under an operation \(\texttt{op}\):
    • Given instances \(M_1\) and \(M_2\) of the model, we can construct a new instance \(M\) of the same model such that: \[L(M) = L(M_1)\; \texttt{op}\; L(M_2)\]
    • We proved DFA-decidable languages are closed under complement, union, and intersection.
    • We proved NFA-decidable languages are closed under union, concatenation, and Kleene star. (And observed this holds trivially for regex’s as well.)
  • We now turn to comparing the computational power of different models.
    • If any regex can be simulated by an NFA, then NFAs are at least as powerful as regexes. \[ \text{"For all regexes } R, \text{ there is an NFA } N \text{ such that } L(R) = L(N)\text{"} \]
    • If any NFA can be simulated by a regex, then regexes are at least as powerful as NFAs.
    • If both of these are true (they are!), then NFAs and regexes have equivalent power.

Simple example: NFAs are at least as powerful as DFAs

  • NFAs are generalizations of DFAs, so any DFA can be trivially simulated by an NFA.
  • A DFA is just an NFA with:
    • no \(\emptystring\) transitions and
    • exactly one transition for each state and symbol pair.
  • To prove this formally:
    • Let \(D = (Q_D, \Sigma, \delta_D, s_D, F_D)\) be a DFA
    • We can construct an equivalent NFA \(N = (Q_N, \Sigma, \Delta_N, s_N, F_N)\) such that:
      • \(Q_N = Q_D\)
      • \(\Delta_N(q, a) = \fragment{\{\delta_D(q, a)\}}\) for all \(q \in Q_N\) and \(a \in \Sigma\)
      • \(s_N = s_D\)
      • \(F_N = F_D\)
    • Then \(q_0,q_1,\dots,q_n\) is an accepting computation sequence for \(x \in \Sigma^n\) in \(D\) if and only if it is a accepting computation sequence for \(x\) in \(N\): \(q_0=s\), \(q_n \in F\), and for all \(i\), \(q_{i+1} = \delta_D(q_i, x[i+1])\) if and only if \(q_{i+1} \in \Delta_N(q_i, x[i+1])\).
    • Therefore \(L(D) = L(N)\).

Simulating NFAs

  • Trying to simulate an NFA with a DFA is less straight-forward.

  • Warm-up: which of the following are equivalent to plain NFAs?

    • MultistartNFA: an NFA allowing more than one start state
    • SingleAcceptNFA: an NFA allowing only a single accept state
    • InjectiveNFA: an NFA for which the destination of each transition rule is either \(\emptyset\) or a singleton \(\{q_i\}\)
    • NoLoopNFA: an NFA for which no transition rule is of the form \(q \stackrel{a}{\rightarrow} q\) for any \((q, a) \in Q \times \Sigma\)
    • NoEpsilonNFA: an NFA with no \(\emptystring\) transitions.

Recall: Operation of an NFA

data/images/nfa/n1/diagram.svg

Example processing the string 010110:

010110

  • Begin in the state(s) \(\{q_1\}\).

    data/images/nfa/n1/state_q1.svg
  • Read 0 and transition to state(s) \(\{q_1\}\).

    010110

    data/images/nfa/n1/state_q1.svg
  • Read 1 and transition to state(s) \(\{q_1, q_2, q_3\}\).

    010110

    data/images/nfa/n1/state_q1_q2_q3.svg
  • Read 0 and transition to state(s) \(\{q_1, q_3\}\).

    010110

    data/images/nfa/n1/state_q1_q3.svg
  • Read 1 and transition to state(s) \(\{q_1, q_2, q_3, q_4\}\).

    010110

    data/images/nfa/n1/state_q1_q2_q3_q4.svg
  • Read 1 and transition to state(s) \(\{q_1, q_2, q_3, q_4\}\).

    010110

  • Read 0 and transition to state(s) \(\{q_1, q_3, q_4\}\).

    010110

    data/images/nfa/n1/state_q1_q3_q4.svg
  • Since an accept state is highlighted, the NFA accepts the string.

What would a DFA need to keep track of in order to simulate this NFA?

Simulating NFAs

To simulate an NFA with \(|Q|\) states operating on a string \(w\), how many distinct sets of states must we keep track of in the worst case?

  • \(|Q|^2\)
  • \(2^{|Q|}\)
  • \(|w|^2\)
  • \(2^{|w|}\)

Example NFA to DFA conversion

data/images/nfa/n4_simple.svg
Example NFA (no \(\emptystring\) transitions)
a,b a b a b a b a,b a b a b b a

Example NFA to DFA conversion

data/images/nfa/n4.svg
Example NFA (with \(\emptystring\) transitions)
a,b a b a b a a b a,b a b a b b a a

  • This process usually does not generate “optimal” DFAs.
  • In this example, we could remove unreachable states.
  • For proving equivalence, we don’t care about optimality!
    So we do not remove states in general.

Example NFA to DFA conversion

data/images/nfa/n4.svg
Example NFA (with \(\emptystring\) transitions)
a,b a b a b a b a,b b a b b a a

  • This process usually does not generate “optimal” DFAs.
  • In this example, we could remove unreachable states.
  • For proving equivalence, we don’t care about optimality!
    So we do not remove states in general.

There exist \(k\)-state NFAs for which the smallest equivalent DFA has \(2^k\) states!
Example near this bound:
\(k^\text{th}\)-to-last symbol NFA

NFA to DFA Conversion: “Subset Construction”

data/images/nfa/n4_simple.svg
data/images/nfa/n4_simple_dfa.svg

Theorem: Every NFA-decidable language is DFA-decidable.

Proof (no \(\emptystring\) transitions)

  • Let \(N = (Q_N, \Sigma, \Delta_N, s_N, F_N)\) be an NFA without \(\emptystring\) transitions.
  • Construct \(D = (Q_D, \Sigma, \delta_D, s_D, F_D)\) as follows:
    • \(Q_D = \powerset(Q_N)\)
    • \(s_D = \{s_N\}\)
    • \(F_D = \fragment{\setbuild{R \in Q_D}{\fragment{R \cap F_N \neq \emptyset}}}\)
    • For all \(R \in Q_D\) and \(b \in \Sigma\): \[\delta_D(R, b) = \bigcup_{q \in R} \Delta_N(q, b)\]
  • What about \(\emptystring\) transitions? Convention:
    • After reading each symbol, jump to all states reachable by any number of \(\emptystring\) transitions.
    • Also do this for the start state.

Following Epsilon Transitions

For any \(R \subseteq Q_N\), define: \[ E(R) = \setbuild{q \in Q_N}{q \text{ is reachable from some $r \in R$ by a sequence of 0 or more $\emptystring$-transitions}} \]

data/images/nfa/multiple-eps-transitions.svg
  • \(E(\{5\}) = \fragment{\{4, 5\}}\)
  • \(E(\{6\}) = \fragment{\{6\}}\)
  • \(E(\{1\}) = \fragment{\{1, 3, 4, 5, 6\}}\)
  • \(E(\{2\}) = \fragment{\{2\}}\)
  • \(E(\{3\}) = \fragment{\{3, 4, 5\}}\)
  • \(E(\{2, 5\}) = \fragment{\{2, 4, 5\}}\)

NFA to DFA Conversion: “Subset Construction”

data/images/nfa/n4.svg
data/images/nfa/n4_dfa_nodeadstates.svg

Theorem: If a language is NFA-decidable, then it is also DFA-decidable.

Proof

  • Let \(N = (Q_N, \Sigma, \Delta_N, s_N, F_N)\) be an NFA.
  • Construct \(D = (Q_D, \Sigma, \delta_D, s_D, F_D)\) as follows:
    • \(Q_D = \powerset(Q_N)\)
    • \(\color{red} s_D = \fragment{E(\{s_N\})}\)
    • \(F_D = \setbuild{R \in Q_D}{R \cap F_N \neq \emptyset}\)
    • For all \(R \in Q_D\) and \(b \in \Sigma\): \[\color{red} \delta_D(R, b) = \fragment{E\left(\bigcup_{q \in R} \Delta_N(q, b)\right)} \fragment{= \bigcup_{q \in R} E(\Delta_N(q, b))}\]

This convention for handling \(\emptystring\)-transitions is not the only possible one!

Corollary: a language is DFA-decidable if and only if it is NFA-decidable.

Equivalence of regular grammars and DFAs

  • Last lecture we proved NFAs and DFAs have equivalent computational power.
    • Any DFA “is” an NFA.
    • Any NFA can be simulated by a DFA using the “subset construction”.
  • What about Context-Free Grammars?
    • CFGs will turn out to be more powerful than DFAs.
    • Any DFA can be “simulated by” a CFG.
    • The CFG-decidable language \(\{0^n 1^n \mid n \geq 0\}\) is not DFA-decidable.

Recall right-regular grammars (RRG)

A right-regular grammar (RRG) is a CFG whose rules are all of the form: \[ X \to a Y \qquad \text{or} \qquad X \to \emptystring \]

Theorem: Every DFA-decidable language is RRG-decidable (and thus CFG-decidable).

Given any DFA \(D\), we can convert it into RRG \(G\) such that \(L(G) = L(D).\) In other words \(G\) generates string \(w\) iff \(D\) accepts \(w\).

Construction Example:

data/images/dfa_rrg/example_dfa/diagram.svg

Equivalent grammar \(G\): \[\begin{align*} P &\to \fragment{0T\;|\;} \fragment{1R} \\ T &\to 0R\;|\; 1P \\ R &\to 0P\;|\; 1R \\ P &\to \emptystring \\ T &\to \emptystring \end{align*}\]

Parse tree for \(0110\):
0 P 1 0 0->1 2 T 0->2 3 1 2->3 4 P 2->4 5 1 4->5 6 R 4->6 7 0 6->7 8 P 6->8

Derivation of \(0110\): \[ \fragment{P} \fragment{\yields 0T} \fragment{\yields 01P} \fragment{\yields 011R} \fragment{\yields 0110P} \fragment{\yields 0110} \]

Computation sequence accepting \(0011\): \[ p, t, p, r, p \]

  • Intermediate strings of the derivation have a single nonterminal end representing the current “state”.
  • This nonterminal can be removed only if it represents an accept state.

Simulating DFAs with RRGs

A right-regular grammar (RRG) is a CFG whose rules are all of the form: \[ X \to a Y \qquad \text{or} \qquad X \to \emptystring \]

Theorem: Every DFA-decidable language is RRG-decidable (and thus CFG-decidable).

Given any DFA \(D\), we can convert it into RRG \(G\) such that \(L(G) = L(D).\) In other words \(G\) generates string \(w\) iff \(D\) accepts \(w\).

Proof

  • Let \(D = (Q, \Sigma, \delta, s, F)\) be a DFA.
  • Construct grammar \(G = (\Gamma, \Sigma, S, \rho)\) such that:
    • \(\Gamma=Q\)
    • Start symbol \(S \in \Gamma\) corresponds to start state \(s \in Q\)
    • Production rules \(\rho\) are defined as follows:
      • For each state \(X \in Q\) and \(a \in \Sigma\) add the rule \(X \to a Y\), where \(Y = \delta(X, a)\)
      • For each accept state \(X \in F\), add the rule \(X \to \emptystring\)

We now prove \(L(G) = L(D)\).