handouts/ho03.tex
changeset 490 4fee50f38305
parent 489 e28d7a327870
child 491 d5776c6018f0
equal deleted inserted replaced
489:e28d7a327870 490:4fee50f38305
    38 \item $Q_0 \in Qs$ is the start state,
    38 \item $Q_0 \in Qs$ is the start state,
    39 \item $F \subseteq Qs$ are the accepting states, and
    39 \item $F \subseteq Qs$ are the accepting states, and
    40 \item $\delta$ is the transition function.
    40 \item $\delta$ is the transition function.
    41 \end{itemize}
    41 \end{itemize}
    42 
    42 
    43 \noindent I am sure you have seen this defininition already
    43 \noindent I am sure you have seen this definition already
    44 before. The transition function determines how to ``transition'' from
    44 before. The transition function determines how to ``transition'' from
    45 one state to the next state with respect to a character. We have the
    45 one state to the next state with respect to a character. We have the
    46 assumption that these transition functions do not need to be defined
    46 assumption that these transition functions do not need to be defined
    47 everywhere: so it can be the case that given a character there is no
    47 everywhere: so it can be the case that given a character there is no
    48 next state, in which case we need to raise a kind of ``failure
    48 next state, in which case we need to raise a kind of ``failure
   120 \[
   120 \[
   121 \widehat{\delta}(Q_0, s) \in F 
   121 \widehat{\delta}(Q_0, s) \in F 
   122 \]
   122 \]
   123 
   123 
   124 \noindent I let you think about a definition that describes the set of
   124 \noindent I let you think about a definition that describes the set of
   125 all strings accepted by a determinsitic finite automaton.
   125 all strings accepted by a deterministic finite automaton.
   126 
   126 
   127 \begin{figure}[p]
   127 \begin{figure}[p]
   128 \small
   128 \small
   129 \lstinputlisting[numbers=left,linebackgroundcolor=
   129 \lstinputlisting[numbers=left]{../progs/display/dfa.scala}
   130                   {\ifodd\value{lstnumber}\color{capri!3}\fi}]
       
   131                   {../progs/display/dfa.scala}
       
   132 \caption{A Scala implementation of DFAs using partial functions.
   130 \caption{A Scala implementation of DFAs using partial functions.
   133   Note some subtleties: \texttt{deltas} implements the delta-hat
   131   Note some subtleties: \texttt{deltas} implements the delta-hat
   134   construction by lifting the (partial) transition  function to lists
   132   construction by lifting the (partial) transition  function to lists
   135   of characters. Since \texttt{delta} is given as a partial function,
   133   of characters. Since \texttt{delta} is given as a partial function,
   136   it can obviously go ``wrong'' in which case the \texttt{Try} in
   134   it can obviously go ``wrong'' in which case the \texttt{Try} in
   169 nicely defined by a series of \texttt{case} statements (see Lines 28
   167 nicely defined by a series of \texttt{case} statements (see Lines 28
   170 -- 38 for an example). If you need to represent an automaton with a
   168 -- 38 for an example). If you need to represent an automaton with a
   171 sink state (catch-all-state), you can use Scala's pattern matching and
   169 sink state (catch-all-state), you can use Scala's pattern matching and
   172 write something like
   170 write something like
   173 
   171 
   174 {\small\begin{lstlisting}[language=Scala,linebackgroundcolor=
   172 {\small\begin{lstlisting}[language=Scala]
   175                   {\ifodd\value{lstnumber}\color{capri!3}\fi}]
       
   176 abstract class State
   173 abstract class State
   177 ...
   174 ...
   178 case object Sink extends State
   175 case object Sink extends State
   179 
   176 
   180 val delta : (State, Char) :=> State = 
   177 val delta : (State, Char) :=> State = 
   265 return a set of states. I let you think about this representation for
   262 return a set of states. I let you think about this representation for
   266 NFA-transitions and how it corresponds to the relations used in the
   263 NFA-transitions and how it corresponds to the relations used in the
   267 mathematical definition of NFAs. An example of a transition function
   264 mathematical definition of NFAs. An example of a transition function
   268 in Scala for the NFA shown above is
   265 in Scala for the NFA shown above is
   269 
   266 
   270 {\small\begin{lstlisting}[language=Scala,linebackgroundcolor=
   267 {\small\begin{lstlisting}[language=Scala]
   271                   {\ifodd\value{lstnumber}\color{capri!3}\fi}]
       
   272 val nfa_delta : (State, Char) :=> Set[State] = 
   268 val nfa_delta : (State, Char) :=> Set[State] = 
   273   { case (Q0, 'a') => Set(Q1, Q2)
   269   { case (Q0, 'a') => Set(Q1, Q2)
   274     case (Q0, 'b') => Set(Q0)
   270     case (Q0, 'b') => Set(Q0)
   275     case (Q1, 'a') => Set(Q1, Q2)
   271     case (Q1, 'a') => Set(Q1, Q2)
   276     case (Q1, 'b') => Set(Q0, Q1) }
   272     case (Q1, 'b') => Set(Q0, Q1) }
   277 \end{lstlisting}}  
   273 \end{lstlisting}}  
   278 
   274 
   279 \noindent Like in the mathematical definition, \texttt{starts} is in
   275 Like in the mathematical definition, \texttt{starts} is in
   280 NFAs a set of states; \texttt{fins} is again a function from states to
   276 NFAs a set of states; \texttt{fins} is again a function from states to
   281 booleans. The \texttt{next} function calculates the set of next states
   277 booleans. The \texttt{next} function calculates the set of next states
   282 reachable from a single state \texttt{q} by a character~\texttt{c}. In
   278 reachable from a single state \texttt{q} by a character~\texttt{c}. In
   283 case there is no such state---the partial transition function is
   279 case there is no such state---the partial transition function is
   284 undefined---the empty set is returned (see function
   280 undefined---the empty set is returned (see function
   285 \texttt{applyOrElse} in Lines 9 and 10). The function \texttt{nexts}
   281 \texttt{applyOrElse} in Lines 9 and 10). The function \texttt{nexts}
   286 just lifts this function to sets of states.
   282 just lifts this function to sets of states.
   287  
   283  
   288 \begin{figure}[p]
   284 \begin{figure}[p]
   289 \small
   285 \small
   290 \lstinputlisting[numbers=left,linebackgroundcolor=
   286 \lstinputlisting[numbers=left]{../progs/display/nfa.scala}
   291                   {\ifodd\value{lstnumber}\color{capri!3}\fi}]
       
   292                   {../progs/display/nfa.scala}
       
   293 \caption{A Scala implementation of NFAs using partial functions.
   287 \caption{A Scala implementation of NFAs using partial functions.
   294   Notice that the function \texttt{accepts} implements the
   288   Notice that the function \texttt{accepts} implements the
   295   acceptance of a string in a breath-first search fashion. This can be a costly
   289   acceptance of a string in a breath-first search fashion. This can be a costly
   296   way of deciding whether a string is accepted or not in applications that need to handle
   290   way of deciding whether a string is accepted or not in applications that need to handle
   297   large NFAs and large inputs.\label{nfa}}
   291   large NFAs and large inputs.\label{nfa}}
   309   search} with \emph{backtracking} in unsuccessful cases. In our
   303   search} with \emph{backtracking} in unsuccessful cases. In our
   310 implementation we can implement a depth-first version of
   304 implementation we can implement a depth-first version of
   311 \texttt{accepts} using Scala's \texttt{exists}-function as follows:
   305 \texttt{accepts} using Scala's \texttt{exists}-function as follows:
   312 
   306 
   313 
   307 
   314 {\small\begin{lstlisting}[language=Scala,linebackgroundcolor=
   308 {\small\begin{lstlisting}[language=Scala]
   315                   {\ifodd\value{lstnumber}\color{capri!3}\fi}]
       
   316 def search(q: A, s: List[C]) : Boolean = s match {
   309 def search(q: A, s: List[C]) : Boolean = s match {
   317   case Nil => fins(q)
   310   case Nil => fins(q)
   318   case c::cs => next(q, c).exists(search(_, cs)) 
   311   case c::cs => next(q, c).exists(search(_, cs)) 
   319 }
   312 }
   320 
   313 
   330 lectures. This depth-first search with backtracking is the reason for
   323 lectures. This depth-first search with backtracking is the reason for
   331 the abysmal performance of some regular expression matchings in Java,
   324 the abysmal performance of some regular expression matchings in Java,
   332 Ruby and Python. I like to show you this in the next two sections.
   325 Ruby and Python. I like to show you this in the next two sections.
   333 
   326 
   334 
   327 
   335 \subsubsection*{Epsilon NFAs}
   328 \subsection*{Epsilon NFAs}
   336 
   329 
   337 In order to get an idea what calculations are performed by Java \&
   330 In order to get an idea what calculations are performed by Java \&
   338 friends, we need a method for transforming a regular expression into
   331 friends, we need a method for transforming a regular expression into
   339 an automaton. This automaton should accept exactly those strings that
   332 an automaton. This automaton should accept exactly those strings that
   340 are accepted by the regular expression.  The simplest and most
   333 are accepted by the regular expression.  The simplest and most
   387 $\epsilon$-transitions mean you do not have to ``consume'' any part of
   380 $\epsilon$-transitions mean you do not have to ``consume'' any part of
   388 the input string, but ``silently'' change to a different state. In
   381 the input string, but ``silently'' change to a different state. In
   389 this example, if you are in the starting state $Q_0$, you can silently
   382 this example, if you are in the starting state $Q_0$, you can silently
   390 move either to $Q_1$ or $Q_2$. You can see that once you are in $Q_1$,
   383 move either to $Q_1$ or $Q_2$. You can see that once you are in $Q_1$,
   391 respectively $Q_2$, you cannot ``go back'' to the other states. So it
   384 respectively $Q_2$, you cannot ``go back'' to the other states. So it
   392 seems allowing $\epsilon$-transitions is a rather substancial
   385 seems allowing $\epsilon$-transitions is a rather substantial
   393 extension to NFAs. On first appearances, $\epsilon$-transitions might
   386 extension to NFAs. On first appearances, $\epsilon$-transitions might
   394 even look rather strange, or even silly. To start with, silent
   387 even look rather strange, or even silly. To start with, silent
   395 transitions make the decision whether a string is accepted by an
   388 transitions make the decision whether a string is accepted by an
   396 automaton even harder: with $\epsilon$NFAs we have to look whether we
   389 automaton even harder: with $\epsilon$NFAs we have to look whether we
   397 can do first some $\epsilon$-transitions and then do a
   390 can do first some $\epsilon$-transitions and then do a
   405 The obvious question is: Do we get anything in return for this hassle
   398 The obvious question is: Do we get anything in return for this hassle
   406 with silent transitions?  Well, we still have to work for it\ldots
   399 with silent transitions?  Well, we still have to work for it\ldots
   407 unfortunately.  If we were to follow the many textbooks on the
   400 unfortunately.  If we were to follow the many textbooks on the
   408 subject, we would now start with defining what $\epsilon$NFAs
   401 subject, we would now start with defining what $\epsilon$NFAs
   409 are---that would require extending the transition relation of
   402 are---that would require extending the transition relation of
   410 NFAs. Next, we woudl show that the $\epsilon$NFAs are equivalent to
   403 NFAs. Next, we would show that the $\epsilon$NFAs are equivalent to
   411 NFAs and so on. Once we have done all this on paper, we would need to
   404 NFAs and so on. Once we have done all this on paper, we would need to
   412 implement $\epsilon$NFAs. Lets try to take a shortcut instead. We are
   405 implement $\epsilon$NFAs. Lets try to take a shortcut instead. We are
   413 not really interested in $\epsilon$NFAs; they are only a convenient
   406 not really interested in $\epsilon$NFAs; they are only a convenient
   414 tool for translating regular expressions into automata. So we are not
   407 tool for translating regular expressions into automata. So we are not
   415 going to implementing them explicitly, but translate them immediately
   408 going to implementing them explicitly, but translate them immediately
   462 by $\epsilon$-transitions.
   455 by $\epsilon$-transitions.
   463 
   456 
   464 
   457 
   465 \begin{figure}[p]
   458 \begin{figure}[p]
   466 \small
   459 \small
   467 \lstinputlisting[numbers=left,linebackgroundcolor=
   460 \lstinputlisting[numbers=left]{../progs/display/enfa.scala}
   468                   {\ifodd\value{lstnumber}\color{capri!3}\fi}]
       
   469                   {../progs/display/enfa.scala}
       
   470 
   461 
   471 \caption{A Scala function that translates $\epsilon$NFA into NFAs. The
   462 \caption{A Scala function that translates $\epsilon$NFA into NFAs. The
   472   transtion function of $\epsilon$NFA takes as input an \texttt{Option[C]}.
   463   transition function of $\epsilon$NFA takes as input an \texttt{Option[C]}.
   473   \texttt{None} stands for an $\epsilon$-transition; \texttt{Some(c)}
   464   \texttt{None} stands for an $\epsilon$-transition; \texttt{Some(c)}
   474   for a ``proper'' transition consuming a character. The functions in
   465   for a ``proper'' transition consuming a character. The functions in
   475   Lines 18--26 calculate
   466   Lines 18--26 calculate
   476   all states reachable by one or more $\epsilon$-transition for a given
   467   all states reachable by one or more $\epsilon$-transition for a given
   477   set of states. The NFA is constructed in Lines 36--38.\label{enfa}}
   468   set of states. The NFA is constructed in Lines 36--38.\label{enfa}}
   478 \end{figure}
   469 \end{figure}
   479 
   470 
   480 Also look carefully how the transitions of $\epsilon$NFAs are
   471 Also look carefully how the transitions of $\epsilon$NFAs are
   481 implemented. The additional possibility of performing silent
   472 implemented. The additional possibility of performing silent
   482 transitions is encoded by using \texttt{Option[C]} as the type for the
   473 transitions is encoded by using \texttt{Option[C]} as the type for the
   483 ``input''. The \texttt{Some}s stand for ``propper'' transitions where
   474 ``input''. The \texttt{Some}s stand for ``proper'' transitions where
   484 a character is consumed; \texttt{None} stands for
   475 a character is consumed; \texttt{None} stands for
   485 $\epsilon$-transitions. The transition functions for the two
   476 $\epsilon$-transitions. The transition functions for the two
   486 $\epsilon$NFAs from the beginning of this section can be defined as
   477 $\epsilon$NFAs from the beginning of this section can be defined as
   487 
   478 
   488 {\small\begin{lstlisting}[language=Scala,linebackgroundcolor=
   479 {\small\begin{lstlisting}[language=Scala]
   489                   {\ifodd\value{lstnumber}\color{capri!3}\fi}]
       
   490 val enfa_trans1 : (State, Option[Char]) :=> Set[State] = 
   480 val enfa_trans1 : (State, Option[Char]) :=> Set[State] = 
   491   { case (Q0, Some('a')) => Set(Q0)
   481   { case (Q0, Some('a')) => Set(Q0)
   492     case (Q0, None) => Set(Q1, Q2)
   482     case (Q0, None) => Set(Q1, Q2)
   493     case (Q1, Some('a')) => Set(Q1)
   483     case (Q1, Some('a')) => Set(Q1)
   494     case (Q2, Some('b')) => Set(Q2) }
   484     case (Q2, Some('b')) => Set(Q2) }
   501 
   491 
   502 \noindent
   492 \noindent
   503 I hope you agree now with my earlier statement that the $\epsilon$NFAs
   493 I hope you agree now with my earlier statement that the $\epsilon$NFAs
   504 are just an API for NFAs.
   494 are just an API for NFAs.
   505 
   495 
   506 \subsubsection*{Thompson Construction}
   496 \subsection*{Thompson Construction}
   507 
   497 
   508 Having the translation of $\epsilon$NFAs to NFAs in place, we can
   498 Having the translation of $\epsilon$NFAs to NFAs in place, we can
   509 finally return to the problem of translating regular expressions into
   499 finally return to the problem of translating regular expressions into
   510 equivalent NFAs. Recall that by equivalent we mean that the NFAs
   500 equivalent NFAs. Recall that by equivalent we mean that the NFAs
   511 recognise the same language. Consider the simple regular expressions
   501 recognise the same language. Consider the simple regular expressions
   541 increases this counter whenever a new state is created.\footnote{You might
   531 increases this counter whenever a new state is created.\footnote{You might
   542   have to read up what \emph{companion objects} do in Scala.}
   532   have to read up what \emph{companion objects} do in Scala.}
   543 
   533 
   544 \begin{figure}[p]
   534 \begin{figure}[p]
   545 \small
   535 \small
   546 \lstinputlisting[numbers=left,linebackgroundcolor=
   536 \lstinputlisting[numbers=left]{../progs/display/thompson1.scala}
   547                   {\ifodd\value{lstnumber}\color{capri!3}\fi}]
       
   548                   {../progs/display/thompson1.scala}
       
   549 \caption{The first part of the Thompson Construction. Lines 7--16
   537 \caption{The first part of the Thompson Construction. Lines 7--16
   550   implement a way of how to create new states that are all
   538   implement a way of how to create new states that are all
   551   distinct by virtue of a counter. This counter is
   539   distinct by virtue of a counter. This counter is
   552   increased in the companion object of \texttt{TState}
   540   increased in the companion object of \texttt{TState}
   553   whenever a new state is created. The code in Lines 24--40
   541   whenever a new state is created. The code in Lines 24--40
   556   \label{thompson1}}
   544   \label{thompson1}}
   557 \end{figure}
   545 \end{figure}
   558 
   546 
   559 \begin{figure}[p]
   547 \begin{figure}[p]
   560 \small
   548 \small
   561 \lstinputlisting[numbers=left,linebackgroundcolor=
   549 \lstinputlisting[numbers=left]{../progs/display/thompson2.scala}
   562                   {\ifodd\value{lstnumber}\color{capri!3}\fi}]
       
   563                   {../progs/display/thompson2.scala}
       
   564 \caption{The second part of the Thompson Construction implementing
   550 \caption{The second part of the Thompson Construction implementing
   565   the composition of NFAs according to $\cdot$, $+$ and $\_^*$.
   551   the composition of NFAs according to $\cdot$, $+$ and ${}^*$.
   566   The implicit class about rich partial functions
   552   The implicit class about rich partial functions
   567   implements the infix operation \texttt{+++} which
   553   implements the infix operation \texttt{+++} which
   568   combines an $\epsilon$NFA transition with a NFA transition
   554   combines an $\epsilon$NFA transition with a NFA transition
   569   (both given as partial functions).\label{thompson2}}
   555   (both given as partial functions).\label{thompson2}}
   570 \end{figure}
   556 \end{figure}
   648 
   634 
   649 \noindent The idea behind this construction is that the start of any
   635 \noindent The idea behind this construction is that the start of any
   650 string is first recognised by the first NFA, then we silently change
   636 string is first recognised by the first NFA, then we silently change
   651 to the second NFA; the ending of the string is recognised by the
   637 to the second NFA; the ending of the string is recognised by the
   652 second NFA...just like matching of a string by the regular expression
   638 second NFA...just like matching of a string by the regular expression
   653 $r_1\cdot r_2$. The Scala code for this constrction is given in
   639 $r_1\cdot r_2$. The Scala code for this construction is given in
   654 Figure~\ref{thompson2} in Lines 16--23. The starting states of the
   640 Figure~\ref{thompson2} in Lines 16--23. The starting states of the
   655 $\epsilon$NFA are the starting states of the first NFA (corresponding
   641 $\epsilon$NFA are the starting states of the first NFA (corresponding
   656 to $r_1$); the accepting function is the accepting function of the
   642 to $r_1$); the accepting function is the accepting function of the
   657 second NFA (corresponding to $r_2$). The new transition function is
   643 second NFA (corresponding to $r_2$). The new transition function is
   658 all the ``old'' transitions plus the $\epsilon$-transitions connecting
   644 all the ``old'' transitions plus the $\epsilon$-transitions connecting
   659 the accepting states of the first NFA to the starting states of the
   645 the accepting states of the first NFA to the starting states of the
   660 first NFA (Lines 18 and 19). The $\epsilon$NFA is then immedately
   646 first NFA (Lines 18 and 19). The $\epsilon$NFA is then immediately
   661 translated in a NFA.
   647 translated in a NFA.
   662 
   648 
   663 
   649 
   664 The case for the choice regular expression $r_1 + r_2$ is slightly
   650 The case for the alternative regular expression $r_1 + r_2$ is
   665 different: We are given by recursion two NFAs representing $r_1$ and
   651 slightly different: We are given by recursion two NFAs representing
   666 $r_2$ respectively.
   652 $r_1$ and $r_2$ respectively. Each NFA has some starting states and
   667 
   653 some accepting states. We obtain a NFA for the regular expression $r_1
   668 \begin{center}
   654 + r_2$ by composing the transition functions (this crucially depends
       
   655 on knowing that the states of each component NFA are distinct); and
       
   656 also combine the starting states and accepting functions.
       
   657 
       
   658 \begin{center}
       
   659 \begin{tabular}[t]{ccc}
   669 \begin{tikzpicture}[node distance=3mm,
   660 \begin{tikzpicture}[node distance=3mm,
   670     >=stealth',very thick,
   661     >=stealth',very thick,
   671     every state/.style={minimum size=3pt,draw=blue!50,very thick,fill=blue!20},]
   662     every state/.style={minimum size=3pt,draw=blue!50,very thick,fill=blue!20},
       
   663     baseline=(current bounding box.center)]
   672 \node at (0,0)  (1)  {$\mbox{}$};
   664 \node at (0,0)  (1)  {$\mbox{}$};
   673 \node  (2)  [above=10mm of 1] {};
   665 \node  (2)  [above=10mm of 1] {};
   674 \node[state, initial]  (4)  [above=1mm of 2] {$\mbox{}$};
   666 \node[state, initial]  (4)  [above=1mm of 2] {$\mbox{}$};
   675 \node[state, initial]  (5)  [below=1mm of 2] {$\mbox{}$};
   667 \node[state, initial]  (5)  [below=1mm of 2] {$\mbox{}$};
   676 \node[state, initial]  (3)  [below=10mm of 1] {$\mbox{}$};
   668 \node[state, initial]  (3)  [below=10mm of 1] {$\mbox{}$};
   689 \node (2) [rounded corners, inner sep=1mm, thick, draw=black!60, fill=black!20, fit= (3) (b1) (b2) (b3)] {};
   681 \node (2) [rounded corners, inner sep=1mm, thick, draw=black!60, fill=black!20, fit= (3) (b1) (b2) (b3)] {};
   690 \node [yshift=3mm] at (1.north) {$r_1$};
   682 \node [yshift=3mm] at (1.north) {$r_1$};
   691 \node [yshift=3mm] at (2.north) {$r_2$};
   683 \node [yshift=3mm] at (2.north) {$r_2$};
   692 \end{pgfonlayer}
   684 \end{pgfonlayer}
   693 \end{tikzpicture}
   685 \end{tikzpicture}
   694 \end{center}
   686 &
   695 
   687 \mbox{}\qquad\tikz{\draw[>=stealth,line width=2mm,->] (0,0) -- (1, 0)}\quad\mbox{}
   696 \noindent Each NFA has some starting states and some accepting
   688 &
   697 states. We obtain a NFA for the regular expression $r_1 + r_2$
       
   698 by composing the transition functions (this crucially depends
       
   699 on knowing that the states of each component NFA are distinct);
       
   700 and also combine the starting states and accepting functions:
       
   701 
       
   702 \begin{center}
       
   703 \begin{tikzpicture}[node distance=3mm,
   689 \begin{tikzpicture}[node distance=3mm,
   704     >=stealth',very thick,
   690     >=stealth',very thick,
   705     every state/.style={minimum size=3pt,draw=blue!50,very thick,fill=blue!20},]
   691     every state/.style={minimum size=3pt,draw=blue!50,very thick,fill=blue!20},
       
   692     baseline=(current bounding box.center)]
   706 \node at (0,0) (1)  {$\mbox{}$};
   693 \node at (0,0) (1)  {$\mbox{}$};
   707 \node (2)  [above=10mm of 1] {$$};
   694 \node (2)  [above=10mm of 1] {$$};
   708 \node[state, initial]  (4)  [above=1mm of 2] {$\mbox{}$};
   695 \node[state, initial]  (4)  [above=1mm of 2] {$\mbox{}$};
   709 \node[state, initial]  (5)  [below=1mm of 2] {$\mbox{}$};
   696 \node[state, initial]  (5)  [below=1mm of 2] {$\mbox{}$};
   710 \node[state, initial]  (3)  [below=10mm of 1] {$\mbox{}$};
   697 \node[state, initial]  (3)  [below=10mm of 1] {$\mbox{}$};
   725 \begin{pgfonlayer}{background}
   712 \begin{pgfonlayer}{background}
   726 \node (3) [rounded corners, inner sep=1mm, thick, draw=black!60, fill=black!20, fit= (1) (a2) (a3) (b2) (b3)] {};
   713 \node (3) [rounded corners, inner sep=1mm, thick, draw=black!60, fill=black!20, fit= (1) (a2) (a3) (b2) (b3)] {};
   727 \node [yshift=3mm] at (3.north) {$r_1+ r_2$};
   714 \node [yshift=3mm] at (3.north) {$r_1+ r_2$};
   728 \end{pgfonlayer}
   715 \end{pgfonlayer}
   729 \end{tikzpicture}
   716 \end{tikzpicture}
       
   717 \end{tabular}
   730 \end{center}
   718 \end{center}
   731 
   719 
   732 \noindent The code for this construction is in Figure~\ref{thompson2}
   720 \noindent The code for this construction is in Figure~\ref{thompson2}
   733 in Lines 25--33. Finally for the $*$-case we have a NFA for $r$
   721 in Lines 25--33.
   734 
   722 
   735 \begin{center}
   723 Finally for the $*$-case we have a NFA for $r$ and connect its
       
   724 accepting states to a new starting state via
       
   725 $\epsilon$-transitions. This new starting state is also an accepting
       
   726 state, because $r^*$ can recognise the empty string.
       
   727 
       
   728 \begin{center}
       
   729 \begin{tabular}[b]{@{\hspace{-4mm}}ccc@{}}  
   736 \begin{tikzpicture}[node distance=3mm,
   730 \begin{tikzpicture}[node distance=3mm,
   737                              >=stealth',very thick, every state/.style={minimum size=3pt,draw=blue!50,very thick,fill=blue!20},]
   731     >=stealth',very thick,
       
   732     every state/.style={minimum size=3pt,draw=blue!50,very thick,fill=blue!20},
       
   733     baseline=(current bounding box.north)]
   738 \node at (0,0)  (1)  {$\mbox{}$};
   734 \node at (0,0)  (1)  {$\mbox{}$};
   739 \node[state, initial]  (2)  [right=16mm of 1] {$\mbox{}$};
   735 \node[state, initial]  (2)  [right=16mm of 1] {$\mbox{}$};
   740 \node (a)  [right=of 2] {$\ldots$};
   736 \node (a)  [right=of 2] {$\ldots$};
   741 \node[state, accepting]  (a1)  [right=of a] {$\mbox{}$};
   737 \node[state, accepting]  (a1)  [right=of a] {$\mbox{}$};
   742 \node[state, accepting]  (a2)  [above=of a1] {$\mbox{}$};
   738 \node[state, accepting]  (a2)  [above=of a1] {$\mbox{}$};
   744 \begin{pgfonlayer}{background}
   740 \begin{pgfonlayer}{background}
   745 \node (1) [rounded corners, inner sep=1mm, thick, draw=black!60, fill=black!20, fit= (2) (a1) (a2) (a3)] {};
   741 \node (1) [rounded corners, inner sep=1mm, thick, draw=black!60, fill=black!20, fit= (2) (a1) (a2) (a3)] {};
   746 \node [yshift=3mm] at (1.north) {$r$};
   742 \node [yshift=3mm] at (1.north) {$r$};
   747 \end{pgfonlayer}
   743 \end{pgfonlayer}
   748 \end{tikzpicture}
   744 \end{tikzpicture}
   749 \end{center}
   745 &
   750 
   746 \raisebox{-16mm}{\;\tikz{\draw[>=stealth,line width=2mm,->] (0,0) -- (1, 0)}}
   751 \noindent and connect its accepting states to a new starting state via
   747 &
   752 $\epsilon$-transitions. This new starting state is also an accepting
       
   753 state, because $r^*$ can recognise the empty string. This gives the
       
   754 following $\epsilon$NFA for $r^*$ (the corresponding code is in
       
   755 Figure~\ref{thompson2} in Lines 35--43:
       
   756 
       
   757 \begin{center}  
       
   758 \begin{tikzpicture}[node distance=3mm,
   748 \begin{tikzpicture}[node distance=3mm,
   759     >=stealth',very thick,
   749     >=stealth',very thick,
   760     every state/.style={minimum size=3pt,draw=blue!50,very thick,fill=blue!20},]
   750     every state/.style={minimum size=3pt,draw=blue!50,very thick,fill=blue!20},
       
   751     baseline=(current bounding box.north)]
   761 \node at (0,0) [state, initial,accepting]  (1)  {$\mbox{}$};
   752 \node at (0,0) [state, initial,accepting]  (1)  {$\mbox{}$};
   762 \node[state]  (2)  [right=16mm of 1] {$\mbox{}$};
   753 \node[state]  (2)  [right=16mm of 1] {$\mbox{}$};
   763 \node (a)  [right=of 2] {$\ldots$};
   754 \node (a)  [right=of 2] {$\ldots$};
   764 \node[state]  (a1)  [right=of a] {$\mbox{}$};
   755 \node[state]  (a1)  [right=of a] {$\mbox{}$};
   765 \node[state]  (a2)  [above=of a1] {$\mbox{}$};
   756 \node[state]  (a2)  [above=of a1] {$\mbox{}$};
   770 \path[->] (a3) edge [bend left=45] node [below]  {$\epsilon$} (1);
   761 \path[->] (a3) edge [bend left=45] node [below]  {$\epsilon$} (1);
   771 \begin{pgfonlayer}{background}
   762 \begin{pgfonlayer}{background}
   772 \node (2) [rounded corners, inner sep=1mm, thick, draw=black!60, fill=black!20, fit= (1) (a2) (a3)] {};
   763 \node (2) [rounded corners, inner sep=1mm, thick, draw=black!60, fill=black!20, fit= (1) (a2) (a3)] {};
   773 \node [yshift=3mm] at (2.north) {$r^*$};
   764 \node [yshift=3mm] at (2.north) {$r^*$};
   774 \end{pgfonlayer}
   765 \end{pgfonlayer}
   775 \end{tikzpicture}
   766 \end{tikzpicture}    
   776 \end{center}
   767 \end{tabular}
   777 
   768 \end{center}
   778 
   769 
   779 To sum ap, you can see in the sequence and star cases the need of
   770 \noindent 
       
   771 The corresponding code is in Figure~\ref{thompson2} in Lines 35--43)
       
   772 
       
   773 To sum up, you can see in the sequence and star cases the need of
   780 having silent $\epsilon$-transitions. Similarly the alternative case
   774 having silent $\epsilon$-transitions. Similarly the alternative case
   781 shows the need of the NFA-nondeterminsim. It seems awkward to form the
   775 shows the need of the NFA-nondeterminism. It seems awkward to form the
   782 `alternative' composition of two DFAs, because DFA do not allow
   776 `alternative' composition of two DFAs, because DFA do not allow
   783 several starting and successor states. All these constructions can now
   777 several starting and successor states. All these constructions can now
   784 be put together in the following recursive function:
   778 be put together in the following recursive function:
   785 
   779 
   786 
   780 
   787 {\small\begin{lstlisting}[language=Scala,linebackgroundcolor=
   781 {\small\begin{lstlisting}[language=Scala]
   788                   {\ifodd\value{lstnumber}\color{capri!3}\fi}]
   782 def thompson(r: Rexp) : NFAt = r match {
   789 def thompson (r: Rexp) : NFAt = r match {
       
   790   case ZERO => NFA_ZERO()
   783   case ZERO => NFA_ZERO()
   791   case ONE => NFA_ONE()
   784   case ONE => NFA_ONE()
   792   case CHAR(c) => NFA_CHAR(c)  
   785   case CHAR(c) => NFA_CHAR(c)  
   793   case ALT(r1, r2) => NFA_ALT(thompson(r1), thompson(r2))
   786   case ALT(r1, r2) => NFA_ALT(thompson(r1), thompson(r2))
   794   case SEQ(r1, r2) => NFA_SEQ(thompson(r1), thompson(r2))
   787   case SEQ(r1, r2) => NFA_SEQ(thompson(r1), thompson(r2))
   795   case STAR(r1) => NFA_STAR(thompson(r1))
   788   case STAR(r1) => NFA_STAR(thompson(r1))
   796 }
   789 }
   797 \end{lstlisting}}
   790 \end{lstlisting}}
   798 
   791 
   799 \noindent
   792 \noindent
   800 It calculates a NFA from a regular expressions. At last we can run a
   793 It calculates a NFA from a regular expressions. At last we can run
   801 NFA for the our evil regular expression examples.
   794 NFAs for the our evil regular expression examples.  The graph on the
       
   795 left shows that when translating a regular expression such as
       
   796 $a^{\{n\}}$ into a NFA, the size can blow up and then even the
       
   797 relative fast (on small examples) breadth-first search can be
       
   798 slow. The graph on the right shows that with `evil' regular
       
   799 expressions the depth-first search can be abysmally slow. Even if the
       
   800 graphs not completely overlap with the curves of Python, Ruby and
       
   801 Java, they are similar enough. OK\ldots now you know why regular
       
   802 expression matchers in those languages are so slow. 
   802 
   803 
   803 
   804 
   804 \begin{center}
   805 \begin{center}
   805 \begin{tabular}{@{\hspace{-1mm}}c@{\hspace{1mm}}c@{}}  
   806 \begin{tabular}{@{\hspace{-1mm}}c@{\hspace{1mm}}c@{}}  
   806 \begin{tikzpicture}
   807 \begin{tikzpicture}
   807 \begin{axis}[
   808 \begin{axis}[
   808     title={Graph: $\texttt{a?\{n\}\,a{\{n\}}}$ and strings 
   809     title={Graph: $a^{?\{n\}} \cdot a^{\{n\}}$ and strings 
   809            $\underbrace{\texttt{a}\ldots \texttt{a}}_{n}$},
   810            $\underbrace{\texttt{a}\ldots \texttt{a}}_{n}$},
       
   811     title style={yshift=-2ex},
   810     xlabel={$n$},
   812     xlabel={$n$},
   811     x label style={at={(1.05,0.0)}},
   813     x label style={at={(1.05,0.0)}},
   812     ylabel={time in secs},
   814     ylabel={time in secs},
   813     enlargelimits=false,
   815     enlargelimits=false,
   814     xtick={0,5,...,30},
   816     xtick={0,5,...,30},
   816     ymax=35,
   818     ymax=35,
   817     ytick={0,5,...,30},
   819     ytick={0,5,...,30},
   818     scaled ticks=false,
   820     scaled ticks=false,
   819     axis lines=left,
   821     axis lines=left,
   820     width=5.5cm,
   822     width=5.5cm,
   821     height=4.5cm, 
   823     height=4cm, 
   822     legend entries={Python,Ruby},  
   824     legend entries={Python,Ruby, breadth-first NFA},
   823     legend pos=south east,
   825     legend style={at={(0.5,-0.25)},anchor=north,font=\small},
   824     legend cell align=left]
   826     legend cell align=left]
   825   \addplot[blue,mark=*, mark options={fill=white}] table {re-python.data};
   827   \addplot[blue,mark=*, mark options={fill=white}] table {re-python.data};
   826   \addplot[brown,mark=triangle*, mark options={fill=white}] table {re-ruby.data};
   828   \addplot[brown,mark=triangle*, mark options={fill=white}] table {re-ruby.data};
   827   % breath-first search in NFAs
   829   % breath-first search in NFAs
   828   \addplot[red,mark=*, mark options={fill=white}] table {
   830   \addplot[red,mark=*, mark options={fill=white}] table {
   843 \end{axis}
   845 \end{axis}
   844 \end{tikzpicture}
   846 \end{tikzpicture}
   845 &
   847 &
   846 \begin{tikzpicture}
   848 \begin{tikzpicture}
   847 \begin{axis}[
   849 \begin{axis}[
   848     title={Graph: $\texttt{(a*)*\,b}$ and strings 
   850     title={Graph: $(a^*)^* \cdot b$ and strings 
   849            $\underbrace{\texttt{a}\ldots \texttt{a}}_{n}$},
   851            $\underbrace{\texttt{a}\ldots \texttt{a}}_{n}$},
       
   852     title style={yshift=-2ex},
   850     xlabel={$n$},
   853     xlabel={$n$},
   851     x label style={at={(1.05,0.0)}},
   854     x label style={at={(1.05,0.0)}},
   852     ylabel={time in secs},
   855     ylabel={time in secs},
   853     enlargelimits=false,
   856     enlargelimits=false,
   854     xtick={0,5,...,30},
   857     xtick={0,5,...,30},
   856     ymax=35,
   859     ymax=35,
   857     ytick={0,5,...,30},
   860     ytick={0,5,...,30},
   858     scaled ticks=false,
   861     scaled ticks=false,
   859     axis lines=left,
   862     axis lines=left,
   860     width=5.5cm,
   863     width=5.5cm,
   861     height=4.5cm, 
   864     height=4cm, 
   862     legend entries={Python, Java},  
   865     legend entries={Python, Java, depth-first NFA},
   863     legend pos=outer north east,
   866     legend style={at={(0.5,-0.25)},anchor=north,font=\small},
   864     legend cell align=left]
   867     legend cell align=left]
   865   \addplot[blue,mark=*, mark options={fill=white}] table {re-python2.data};
   868   \addplot[blue,mark=*, mark options={fill=white}] table {re-python2.data};
   866   \addplot[cyan,mark=*, mark options={fill=white}] table {re-java.data};  
   869   \addplot[cyan,mark=*, mark options={fill=white}] table {re-java.data};  
   867   % depth-first search in NFAs
   870   % depth-first search in NFAs
   868   \addplot[red,mark=*, mark options={fill=white}] table {
   871   \addplot[red,mark=*, mark options={fill=white}] table {
   879 \end{tabular}
   882 \end{tabular}
   880 \end{center}
   883 \end{center}
   881 
   884 
   882 
   885 
   883 
   886 
   884 \subsubsection*{Subset Construction}
   887 \subsection*{Subset Construction}
   885 
   888 
   886 Remember that we did not bother with defining and implementing
   889 Of course, some developers of regular expression matchers are aware
   887 $\epsilon$NFA; we immediately translated them into equivalent
   890 of these problems with sluggish NFAs and try to address them. One
   888 NFAs. Equivalent in the sense of accepting the same language (though
   891 common technique for this I like to show you in this section. It will
   889 we only claimed this and did not prove it rigorously). Remember also
   892 also explain why we insisted on polymorphic types in our DFA code
   890 that NFAs have a non-deterministic transitions, given as a relation.
   893 (remember I used \texttt{A} and \texttt{C} for the types of states and
   891 This non-determinism makes it harder to decide when a string is
   894 the input, see Figure~\ref{dfa} on Page~\pageref{dfa}).\bigskip
   892 accepted or not; such a decision is rather straightforward with DFAs
   895 
   893 (remember their transition function).
   896 \noindent
   894 
   897 To start, remember that we did not bother with defining and
   895 What is interesting is that for every NFA we can find a DFA that also
   898 implementing $\epsilon$NFA; we immediately translated them into
   896 recognises the same language. This might sound like a bit paradoxical,
   899 equivalent NFAs. Equivalent in the sense of accepting the same
   897 but I litke to show you this next. There are a number of ways of
   900 language (though we only claimed this and did not prove it
   898 transforming a NFA into an equivalent DFA, but the most famous is
   901 rigorously). Remember also that NFAs have non-deterministic
   899 \emph{subset construction}. Consider again the NFA below on the left,
   902 transitions defined as a relation or implemented as function returning
   900 consisting of nodes labelled, say, with $0$, $1$ and $2$.
   903 sets of states.  This non-determinism is crucial for the Thompson
       
   904 Construction to work (recall the cases for $\cdot$, $+$ and
       
   905 ${}^*$). But this non-determinism makes it harder with NFAs to decide
       
   906 when a string is accepted or not; such a decision is rather
       
   907 straightforward with DFAs: recall their transition function is a
       
   908 \emph{function} that returns a single state. So we do not have to
       
   909 search at all.  What is perhaps interesting is the fact that for every
       
   910 NFA we can find a DFA that also recognises the same language. This
       
   911 might sound a bit paradoxical: NFA $\rightarrow$ decision of
       
   912 acceptance hard; DFA $\rightarrow$ decision easy. But this \emph{is}
       
   913 true\ldots but of course there is always a caveat---nothing is ever
       
   914 for free in life.
       
   915 
       
   916 There are a number of techniques for transforming a NFA into an
       
   917 equivalent DFA, but the most famous one is the \emph{subset
       
   918   construction}. Consider the following NFA where the states are
       
   919 labelled with, say, $0$, $1$ and $2$.
   901 
   920 
   902 \begin{center}
   921 \begin{center}
   903 \begin{tabular}{c@{\hspace{10mm}}c}
   922 \begin{tabular}{c@{\hspace{10mm}}c}
   904 \begin{tikzpicture}[scale=0.7,>=stealth',very thick,
   923 \begin{tikzpicture}[scale=0.7,>=stealth',very thick,
   905                     every state/.style={minimum size=0pt,
   924                     every state/.style={minimum size=0pt,
   906                     draw=blue!50,very thick,fill=blue!20},
   925                     draw=blue!50,very thick,fill=blue!20},
   907                     baseline=0mm]
   926                     baseline=(current bounding box.center)]
   908 \node[state,initial]  (Q_0)  {$0$};
   927 \node[state,initial]  (Q_0)  {$0$};
   909 \node[state] (Q_1) [above=of Q_0] {$1$};
   928 \node[state] (Q_1) [below=of Q_0] {$1$};
   910 \node[state, accepting] (Q_2) [below=of Q_0] {$2$};
   929 \node[state, accepting] (Q_2) [below=of Q_1] {$2$};
   911 \path[->] (Q_0) edge node [left]  {$\epsilon$} (Q_1);
   930 
   912 \path[->] (Q_0) edge node [left]  {$\epsilon$} (Q_2);
   931 \path[->] (Q_0) edge node [right]  {$b$} (Q_1);
   913 \path[->] (Q_0) edge [loop right] node  {$a$} ();
   932 \path[->] (Q_1) edge node [right]  {$a,b$} (Q_2);
   914 \path[->] (Q_1) edge [loop above] node  {$a$} ();
   933 \path[->] (Q_0) edge [loop above] node  {$a, b$} ();
   915 \path[->] (Q_2) edge [loop below] node  {$b$} ();
       
   916 \end{tikzpicture}
   934 \end{tikzpicture}
   917 &
   935 &
   918 \begin{tabular}{r|cl}
   936 \begin{tabular}{r|ll}
   919 nodes & $a$ & $b$\\
   937 states & $a$ & $b$\\
   920 \hline
   938 \hline
   921 $\{\}\phantom{\star}$ & $\{\}$ & $\{\}$\\
   939 $\{\}\phantom{\star}$ & $\{\}$ & $\{\}$\\
   922 $\{0\}\phantom{\star}$       & $\{0,1,2\}$   & $\{2\}$\\
   940 start: $\{0\}\phantom{\star}$       & $\{0\}$   & $\{0,1\}$\\
   923 $\{1\}\phantom{\star}$       & $\{1\}$       & $\{\}$\\
   941 $\{1\}\phantom{\star}$       & $\{2\}$       & $\{2\}$\\
   924 $\{2\}\star$  & $\{\}$ & $\{2\}$\\
   942 $\{2\}\star$  & $\{\}$ & $\{\}$\\
   925 $\{0,1\}\phantom{\star}$     & $\{0,1,2\}$   & $\{2\}$\\
   943 $\{0,1\}\phantom{\star}$     & $\{0,2\}$   & $\{0,1,2\}$\\
   926 $\{0,2\}\star$ & $\{0,1,2\}$   & $\{2\}$\\
   944 $\{0,2\}\star$ & $\{0\}$   & $\{0,1\}$\\
   927 $\{1,2\}\star$ & $\{1\}$       & $\{2\}$\\
   945 $\{1,2\}\star$ & $\{2\}$       & $\{2\}$\\
   928 s: $\{0,1,2\}\star$ & $\{0,1,2\}$ & $\{2\}$\\
   946 $\{0,1,2\}\star$ & $\{0,2\}$ & $\{0,1,2\}$\\
   929 \end{tabular}
   947 \end{tabular}
   930 \end{tabular}
   948 \end{tabular}
   931 \end{center}
   949 \end{center}
   932 
   950 
   933 \noindent The nodes of the DFA are given by calculating all
   951 \noindent The states of the corresponding DFA are given by generating
   934 subsets of the set of nodes of the NFA (seen in the nodes
   952 all subsets of the set of states of the NFA (seen in the states column
   935 column on the right). The table shows the transition function
   953 in the table on the right). The other columns define the transition
   936 for the DFA. The first row states that $\{\}$ is the
   954 function for the DFA for input $a$ and $b$. The first row states that
   937 sink node which has transitions for $a$ and $b$ to itself.
   955 $\{\}$ is the sink state which has transitions for $a$ and $b$ to
   938 The next three lines are calculated as follows: 
   956 itself.  The next three lines are calculated as follows:
   939 
   957 
   940 \begin{itemize}
   958 \begin{itemize}
   941 \item suppose you calculate the entry for the transition for
   959 \item Suppose you calculate the entry for the $a$-transition for state
   942       $a$ and the node $\{0\}$
   960   $\{0\}$. Look for all states in the NFA that can be reached by such
   943 \item start from the node $0$ in the NFA
   961   a transition from this state; this is only state $0$; therefore from
   944 \item do as many $\epsilon$-transition as you can obtaining a
   962   state $\{0\}$ we can go to state $\{0\}$ via an $a$-transition.
   945       set of nodes, in this case $\{0,1,2\}$
   963 \item Do the same for the $b$-transition; you can reach states $0$ and
   946 \item filter out all notes that do not allow an $a$-transition
   964   $1$ in the NFA; therefore in the DFA we can go from state $\{0\}$ to
   947       from this set, this excludes $2$ which does not permit a
   965   state $\{0,1\}$ via an $b$-transition.
   948       $a$-transition
   966 \item Continue with the states $\{1\}$ and $\{2\}$.
   949 \item from the remaining set, do as many $\epsilon$-transition
   967 \item Once you filled in the transitions for `simple' state, you only
   950       as you can, this yields again $\{0,1,2\}$     
   968   have to build the union for the compound states $\{0,1\}$, $\{0,2\}$
   951 \item the resulting set specifies the transition from $\{0\}$
   969   and so on. For example for $\{0,1\}$ you take the union of line
   952       when given an $a$ 
   970   $\{0\}$ and line $\{1\}$, which gives $\{0,2\}$ for $a$, and
       
   971   $\{0,1,2\}$ for $b$. And so on.
       
   972 \item The starting state of the DFA can be calculated from the
       
   973   starting states of the NFA, that is in this case $0$. But in general
       
   974   there can be many starting states in the NFA and you would take the
       
   975   corresponding subset as \emph{the} starting state of the DFA.
       
   976 \item The accepting states in the DFA are given by all sets that
       
   977   contain a $2$, which is the only accpting state in this NFA. But
       
   978   again in general if the subset contains an accepting state from the
       
   979   NFA, then the corresponding state in the DFA is accepting as well.
   953 \end{itemize}
   980 \end{itemize}
   954 
   981 
   955 \noindent So the transition from the state $\{0\}$ reading an
   982 \noindent This completes the subset construction. The corresponding
   956 $a$ goes to the state $\{0,1,2\}$. Similarly for the other
   983 DFA for the NFA shown above is:
   957 entries in the rows for $\{0\}$, $\{1\}$ and $\{2\}$. The
   984 
   958 other rows are calculated by just taking the union of the
   985 \begin{center}
   959 single node entries. For example for $a$ and $\{0,1\}$ we need
   986 \begin{tikzpicture}[scale=0.8,>=stealth',very thick,
   960 to take the union of $\{0,1,2\}$ (for $0$) and $\{1\}$ (for
       
   961 $1$). The starting state of the DFA can be calculated from the
       
   962 starting state of the NFA, that is $0$, and then do as many
       
   963 $\epsilon$-transitions as possible. This gives $\{0,1,2\}$
       
   964 which is the starting state of the DFA. The terminal states in
       
   965 the DFA are given by all sets that contain a $2$, which is the
       
   966 terminal state of the NFA. This completes the subset
       
   967 construction. So the corresponding DFA to the NFA from 
       
   968 above is
       
   969 
       
   970 \begin{center}
       
   971 \begin{tikzpicture}[scale=0.7,>=stealth',very thick,
       
   972                     every state/.style={minimum size=0pt,
   987                     every state/.style={minimum size=0pt,
   973                     draw=blue!50,very thick,fill=blue!20},
   988                     draw=blue!50,very thick,fill=blue!20},
   974                     baseline=0mm]
   989                     baseline=0mm]
   975 \node[state,initial,accepting]  (q012)  {$0,1,2$};
   990 \node[state,initial]  (q0)  {$0$};
   976 \node[state,accepting] (q02) [right=of q012] {$0,2$};
   991 \node[state] (q01) [right=of q0] {$0,1$};
   977 \node[state] (q01) [above=of q02] {$0,1$};
   992 \node[state,accepting] (q02) [below=of q01] {$0,2$};
   978 \node[state,accepting] (q12) [below=of q02] {$1,2$};
   993 \node[state,accepting] (q012) [right=of q02] {$0,1,2$};
   979 \node[state] (q0) [right=2cm of q01] {$0$};
   994 \node[state] (q1) [below=0.5cm of q0] {$1$};
   980 \node[state] (q1) [right=2.5cm of q02] {$1$};
   995 \node[state,accepting] (q2) [below=1cm of q1] {$2$};
   981 \node[state,accepting] (q2) [right=1.5cm of q12] {$2$};
   996 \node[state] (qn) [below left=1cm of q2] {$\{\}$};
   982 \node[state] (qn) [right=of q1] {$\{\}$};
   997 \node[state,accepting] (q12) [below right=1cm of q2] {$1,2$};
   983 
   998 
   984 \path[->] (q012) edge [loop below] node  {$a$} ();
   999 \path[->] (q0) edge node [above] {$b$} (q01);
   985 \path[->] (q012) edge node [above]  {$b$} (q2);
  1000 \path[->] (q01) edge node [above] {$b$} (q012);
   986 \path[->] (q12) edge [bend left] node [below,pos=0.4]  {$a$} (q1);
  1001 \path[->] (q0) edge [loop above] node  {$a$} ();
   987 \path[->] (q12) edge node [below]  {$b$} (q2);
  1002 \path[->] (q012) edge [loop right] node  {$b$} ();
   988 \path[->] (q02) edge node [above]  {$a$} (q012);
  1003 \path[->] (q012) edge node [below] {$a$} (q02);
   989 \path[->] (q02) edge [bend left] node [above, pos=0.8]  {$b$} (q2);
  1004 \path[->] (q02) edge node [below] {$a$} (q0);
   990 \path[->] (q01) edge node [below]  {$a$} (q012);
  1005 \path[->] (q01) edge [bend left] node [left]  {$a$} (q02);
   991 \path[->] (q01) edge [bend left] node [above]  {$b$} (q2);
  1006 \path[->] (q02) edge [bend left] node [right]  {$b$} (q01);
   992 \path[->] (q0) edge node [below]  {$a$} (q012);
  1007 \path[->] (q1) edge node [left] {$a,b$} (q2);
   993 \path[->] (q0) edge node [right, pos=0.2]  {$b$} (q2);
  1008 \path[->] (q12) edge node [right] {$a, b$} (q2);
   994 \path[->] (q1) edge [loop above] node  {$a$} ();
  1009 \path[->] (q2) edge node [right] {$a, b$} (qn);
   995 \path[->] (q1) edge node [above]  {$b$} (qn);
  1010 \path[->] (qn) edge [loop left] node  {$a,b$} ();
   996 \path[->] (q2) edge [loop right] node  {$b$} ();
  1011 \end{tikzpicture}
   997 \path[->] (q2) edge node [below]  {$a$} (qn);
  1012 \end{center}
   998 \path[->] (qn) edge [loop above] node  {$a,b$} ();
  1013 
   999 \end{tikzpicture}
  1014 \noindent
  1000 \end{center}
  1015 Please check that this is indeed a DFA. The big question is whether
  1001 
  1016 this DFA can recognise the same language as the NFA we started with.
  1002 
  1017 I let you ponder about this question.
  1003 
  1018 
  1004 There are two points to note: One is that very often the
  1019 
  1005 resulting DFA contains a number of ``dead'' nodes that are
  1020 There are also two points to note: One is that very often the
  1006 never reachable from the starting state. For example
  1021 resulting DFA contains a number of ``dead'' states that are never
  1007 there is no way to reach node $\{0,2\}$ from the starting
  1022 reachable from the starting state. This is obvious in this case, where
  1008 state $\{0,1,2\}$. I let you find the other dead states.
  1023 state $\{1\}$, $\{2\}$, $\{1,2\}$ and $\{\}$ can never be reached from
  1009 In effect the DFA in this example is not a minimal DFA. Such
  1024 the starting state.  In effect the DFA in this example is not a
  1010 dead nodes can be safely removed without changing the language
  1025 \emph{minimal} DFA (more about this in a minute). Such dead states can
  1011 that is recognised by the DFA. Another point is that in some
  1026 be safely removed without changing the language that is recognised by
  1012 cases, however, the subset construction produces a DFA that
  1027 the DFA. Another point is that in some cases, however, the subset
  1013 does \emph{not} contain any dead nodes\ldots{}that means it
  1028 construction produces a DFA that does \emph{not} contain any dead
  1014 calculates a minimal DFA. Which in turn means that in some
  1029 states\ldots{}and further calculates a minimal DFA. Which in turn
  1015 cases the number of nodes by going from NFAs to DFAs
  1030 means that in some cases the number of states can by going from NFAs
  1016 exponentially increases, namely by $2^n$ (which is the number
  1031 to DFAs exponentially increase, namely by $2^n$ (which is the number
  1017 of subsets you can form for $n$ nodes). 
  1032 of subsets you can form for $n$ states).  This blow up the number of
  1018 
  1033 states in the DFA is again bad news for how quickly you can decide
  1019 Removing all the dead states in the automaton above,
  1034 whether a string is accepted by a DFA or not. So the caveat with DFAs
  1020 gives a much more legible automaton, namely
  1035 is that they might make the task of finding the next state trival, but
  1021 
  1036 might require $2^n$ times as many states as a NFA.\bigskip
  1022 \begin{center}
  1037 
  1023 \begin{tikzpicture}[scale=0.7,>=stealth',very thick,
  1038 Lastly, can we 
       
  1039 
       
  1040 {\small\begin{lstlisting}[language=Scala]
       
  1041 def subset[A, C](nfa: NFA[A, C]) : DFA[Set[A], C] = {
       
  1042   DFA(nfa.starts, 
       
  1043       { case (qs, c) => nfa.nexts(qs, c) }, 
       
  1044       _.exists(nfa.fins))
       
  1045 }
       
  1046 \end{lstlisting}}  
       
  1047 
       
  1048 
       
  1049 
       
  1050 \subsection*{DFA Minimisation}
       
  1051 
       
  1052 As seen in the subset construction, the translation 
       
  1053 of a NFA to a DFA can result in a rather ``inefficient'' 
       
  1054 DFA. Meaning there are states that are not needed. A
       
  1055 DFA can be \emph{minimised} by the following algorithm:
       
  1056 
       
  1057 \begin{enumerate}
       
  1058 \item Take all pairs $(q, p)$ with $q \not= p$
       
  1059 \item Mark all pairs that accepting and non-accepting states
       
  1060 \item For all unmarked pairs $(q, p)$ and all characters $c$
       
  1061       test whether 
       
  1062       
       
  1063       \begin{center} 
       
  1064       $(\delta(q, c), \delta(p,c))$ 
       
  1065       \end{center} 
       
  1066       
       
  1067       are marked. If there is one, then also mark $(q, p)$.
       
  1068 \item Repeat last step until no change.
       
  1069 \item All unmarked pairs can be merged.
       
  1070 \end{enumerate}
       
  1071 
       
  1072 \noindent To illustrate this algorithm, consider the following 
       
  1073 DFA.
       
  1074 
       
  1075 \begin{center}
       
  1076 \begin{tikzpicture}[>=stealth',very thick,auto,
  1024                     every state/.style={minimum size=0pt,
  1077                     every state/.style={minimum size=0pt,
  1025                     draw=blue!50,very thick,fill=blue!20},
  1078                     inner sep=2pt,draw=blue!50,very thick,
  1026                     baseline=0mm]
  1079                     fill=blue!20}]
  1027 \node[state,initial,accepting]  (q012)  {$0,1,2$};
  1080 \node[state,initial]  (Q_0)  {$Q_0$};
  1028 \node[state,accepting] (q2) [right=of q012] {$2$};
  1081 \node[state] (Q_1) [right=of Q_0] {$Q_1$};
  1029 \node[state] (qn) [right=of q2] {$\{\}$};
  1082 \node[state] (Q_2) [below right=of Q_0] {$Q_2$};
  1030 
  1083 \node[state] (Q_3) [right=of Q_2] {$Q_3$};
  1031 \path[->] (q012) edge [loop below] node  {$a$} ();
  1084 \node[state, accepting] (Q_4) [right=of Q_1] {$Q_4$};
  1032 \path[->] (q012) edge node [above]  {$b$} (q2);
  1085 \path[->] (Q_0) edge node [above]  {$a$} (Q_1);
  1033 \path[->] (q2) edge [loop below] node  {$b$} ();
  1086 \path[->] (Q_1) edge node [above]  {$a$} (Q_4);
  1034 \path[->] (q2) edge node [below]  {$a$} (qn);
  1087 \path[->] (Q_4) edge [loop right] node  {$a, b$} ();
  1035 \path[->] (qn) edge [loop above] node  {$a,b$} ();
  1088 \path[->] (Q_3) edge node [right]  {$a$} (Q_4);
  1036 \end{tikzpicture}
  1089 \path[->] (Q_2) edge node [above]  {$a$} (Q_3);
  1037 \end{center}
  1090 \path[->] (Q_1) edge node [right]  {$b$} (Q_2);
  1038 
  1091 \path[->] (Q_0) edge node [above]  {$b$} (Q_2);
  1039 \noindent Now the big question is whether this DFA
  1092 \path[->] (Q_2) edge [loop left] node  {$b$} ();
  1040 can recognise the same language as the NFA we started with.
  1093 \path[->] (Q_3) edge [bend left=95, looseness=1.3] node 
  1041 I let you ponder about this question.
  1094   [below]  {$b$} (Q_0);
  1042 
  1095 \end{tikzpicture}
  1043 \subsubsection*{Brzozowski's Method}
  1096 \end{center}
       
  1097 
       
  1098 \noindent In Step 1 and 2 we consider essentially a triangle
       
  1099 of the form
       
  1100 
       
  1101 \begin{center}
       
  1102 \begin{tikzpicture}[scale=0.6,line width=0.8mm]
       
  1103 \draw (0,0) -- (4,0);
       
  1104 \draw (0,1) -- (4,1);
       
  1105 \draw (0,2) -- (3,2);
       
  1106 \draw (0,3) -- (2,3);
       
  1107 \draw (0,4) -- (1,4);
       
  1108 
       
  1109 \draw (0,0) -- (0, 4);
       
  1110 \draw (1,0) -- (1, 4);
       
  1111 \draw (2,0) -- (2, 3);
       
  1112 \draw (3,0) -- (3, 2);
       
  1113 \draw (4,0) -- (4, 1);
       
  1114 
       
  1115 \draw (0.5,-0.5) node {$Q_0$}; 
       
  1116 \draw (1.5,-0.5) node {$Q_1$}; 
       
  1117 \draw (2.5,-0.5) node {$Q_2$}; 
       
  1118 \draw (3.5,-0.5) node {$Q_3$};
       
  1119  
       
  1120 \draw (-0.5, 3.5) node {$Q_1$}; 
       
  1121 \draw (-0.5, 2.5) node {$Q_2$}; 
       
  1122 \draw (-0.5, 1.5) node {$Q_3$}; 
       
  1123 \draw (-0.5, 0.5) node {$Q_4$}; 
       
  1124 
       
  1125 \draw (0.5,0.5) node {\large$\star$}; 
       
  1126 \draw (1.5,0.5) node {\large$\star$}; 
       
  1127 \draw (2.5,0.5) node {\large$\star$}; 
       
  1128 \draw (3.5,0.5) node {\large$\star$};
       
  1129 \end{tikzpicture}
       
  1130 \end{center}
       
  1131 
       
  1132 \noindent where the lower row is filled with stars, because in
       
  1133 the corresponding pairs there is always one state that is
       
  1134 accepting ($Q_4$) and a state that is non-accepting (the other
       
  1135 states).
       
  1136 
       
  1137 Now in Step 3 we need to fill in more stars according whether 
       
  1138 one of the next-state pairs are marked. We have to do this 
       
  1139 for every unmarked field until there is no change anymore.
       
  1140 This gives the triangle
       
  1141 
       
  1142 \begin{center}
       
  1143 \begin{tikzpicture}[scale=0.6,line width=0.8mm]
       
  1144 \draw (0,0) -- (4,0);
       
  1145 \draw (0,1) -- (4,1);
       
  1146 \draw (0,2) -- (3,2);
       
  1147 \draw (0,3) -- (2,3);
       
  1148 \draw (0,4) -- (1,4);
       
  1149 
       
  1150 \draw (0,0) -- (0, 4);
       
  1151 \draw (1,0) -- (1, 4);
       
  1152 \draw (2,0) -- (2, 3);
       
  1153 \draw (3,0) -- (3, 2);
       
  1154 \draw (4,0) -- (4, 1);
       
  1155 
       
  1156 \draw (0.5,-0.5) node {$Q_0$}; 
       
  1157 \draw (1.5,-0.5) node {$Q_1$}; 
       
  1158 \draw (2.5,-0.5) node {$Q_2$}; 
       
  1159 \draw (3.5,-0.5) node {$Q_3$};
       
  1160  
       
  1161 \draw (-0.5, 3.5) node {$Q_1$}; 
       
  1162 \draw (-0.5, 2.5) node {$Q_2$}; 
       
  1163 \draw (-0.5, 1.5) node {$Q_3$}; 
       
  1164 \draw (-0.5, 0.5) node {$Q_4$}; 
       
  1165 
       
  1166 \draw (0.5,0.5) node {\large$\star$}; 
       
  1167 \draw (1.5,0.5) node {\large$\star$}; 
       
  1168 \draw (2.5,0.5) node {\large$\star$}; 
       
  1169 \draw (3.5,0.5) node {\large$\star$};
       
  1170 \draw (0.5,1.5) node {\large$\star$}; 
       
  1171 \draw (2.5,1.5) node {\large$\star$}; 
       
  1172 \draw (0.5,3.5) node {\large$\star$}; 
       
  1173 \draw (1.5,2.5) node {\large$\star$}; 
       
  1174 \end{tikzpicture}
       
  1175 \end{center}
       
  1176 
       
  1177 \noindent which means states $Q_0$ and $Q_2$, as well as $Q_1$
       
  1178 and $Q_3$ can be merged. This gives the following minimal DFA
       
  1179 
       
  1180 \begin{center}
       
  1181 \begin{tikzpicture}[>=stealth',very thick,auto,
       
  1182                     every state/.style={minimum size=0pt,
       
  1183                     inner sep=2pt,draw=blue!50,very thick,
       
  1184                     fill=blue!20}]
       
  1185 \node[state,initial]  (Q_02)  {$Q_{0, 2}$};
       
  1186 \node[state] (Q_13) [right=of Q_02] {$Q_{1, 3}$};
       
  1187 \node[state, accepting] (Q_4) [right=of Q_13] 
       
  1188   {$Q_{4\phantom{,0}}$};
       
  1189 \path[->] (Q_02) edge [bend left] node [above]  {$a$} (Q_13);
       
  1190 \path[->] (Q_13) edge [bend left] node [below]  {$b$} (Q_02);
       
  1191 \path[->] (Q_02) edge [loop below] node  {$b$} ();
       
  1192 \path[->] (Q_13) edge node [above]  {$a$} (Q_4);
       
  1193 \path[->] (Q_4) edge [loop above] node  {$a, b$} ();
       
  1194 \end{tikzpicture}
       
  1195 \end{center}
       
  1196 
       
  1197 
       
  1198 \subsection*{Brzozowski's Method}
  1044 
  1199 
  1045 As said before, we can also go into the other direction---from
  1200 As said before, we can also go into the other direction---from
  1046 DFAs to regular expressions. Brzozowski's method calculates
  1201 DFAs to regular expressions. Brzozowski's method calculates
  1047 a regular expression using familiar transformations for
  1202 a regular expression using familiar transformations for
  1048 solving equational systems. Consider the DFA:
  1203 solving equational systems. Consider the DFA:
  1188 
  1343 
  1189 We should prove that Brzozowski's method really produces
  1344 We should prove that Brzozowski's method really produces
  1190 an equivalent  regular expression for the automaton. But
  1345 an equivalent  regular expression for the automaton. But
  1191 for the purposes of this module, we omit this.
  1346 for the purposes of this module, we omit this.
  1192 
  1347 
  1193 \subsubsection*{Automata Minimization}
  1348 
  1194 
  1349 \subsection*{Regular Languages}
  1195 As seen in the subset construction, the translation 
       
  1196 of a NFA to a DFA can result in a rather ``inefficient'' 
       
  1197 DFA. Meaning there are states that are not needed. A
       
  1198 DFA can be \emph{minimised} by the following algorithm:
       
  1199 
       
  1200 \begin{enumerate}
       
  1201 \item Take all pairs $(q, p)$ with $q \not= p$
       
  1202 \item Mark all pairs that accepting and non-accepting states
       
  1203 \item For all unmarked pairs $(q, p)$ and all characters $c$
       
  1204       test whether 
       
  1205       
       
  1206       \begin{center} 
       
  1207       $(\delta(q, c), \delta(p,c))$ 
       
  1208       \end{center} 
       
  1209       
       
  1210       are marked. If there is one, then also mark $(q, p)$.
       
  1211 \item Repeat last step until no change.
       
  1212 \item All unmarked pairs can be merged.
       
  1213 \end{enumerate}
       
  1214 
       
  1215 \noindent To illustrate this algorithm, consider the following 
       
  1216 DFA.
       
  1217 
       
  1218 \begin{center}
       
  1219 \begin{tikzpicture}[>=stealth',very thick,auto,
       
  1220                     every state/.style={minimum size=0pt,
       
  1221                     inner sep=2pt,draw=blue!50,very thick,
       
  1222                     fill=blue!20}]
       
  1223 \node[state,initial]  (Q_0)  {$Q_0$};
       
  1224 \node[state] (Q_1) [right=of Q_0] {$Q_1$};
       
  1225 \node[state] (Q_2) [below right=of Q_0] {$Q_2$};
       
  1226 \node[state] (Q_3) [right=of Q_2] {$Q_3$};
       
  1227 \node[state, accepting] (Q_4) [right=of Q_1] {$Q_4$};
       
  1228 \path[->] (Q_0) edge node [above]  {$a$} (Q_1);
       
  1229 \path[->] (Q_1) edge node [above]  {$a$} (Q_4);
       
  1230 \path[->] (Q_4) edge [loop right] node  {$a, b$} ();
       
  1231 \path[->] (Q_3) edge node [right]  {$a$} (Q_4);
       
  1232 \path[->] (Q_2) edge node [above]  {$a$} (Q_3);
       
  1233 \path[->] (Q_1) edge node [right]  {$b$} (Q_2);
       
  1234 \path[->] (Q_0) edge node [above]  {$b$} (Q_2);
       
  1235 \path[->] (Q_2) edge [loop left] node  {$b$} ();
       
  1236 \path[->] (Q_3) edge [bend left=95, looseness=1.3] node 
       
  1237   [below]  {$b$} (Q_0);
       
  1238 \end{tikzpicture}
       
  1239 \end{center}
       
  1240 
       
  1241 \noindent In Step 1 and 2 we consider essentially a triangle
       
  1242 of the form
       
  1243 
       
  1244 \begin{center}
       
  1245 \begin{tikzpicture}[scale=0.6,line width=0.8mm]
       
  1246 \draw (0,0) -- (4,0);
       
  1247 \draw (0,1) -- (4,1);
       
  1248 \draw (0,2) -- (3,2);
       
  1249 \draw (0,3) -- (2,3);
       
  1250 \draw (0,4) -- (1,4);
       
  1251 
       
  1252 \draw (0,0) -- (0, 4);
       
  1253 \draw (1,0) -- (1, 4);
       
  1254 \draw (2,0) -- (2, 3);
       
  1255 \draw (3,0) -- (3, 2);
       
  1256 \draw (4,0) -- (4, 1);
       
  1257 
       
  1258 \draw (0.5,-0.5) node {$Q_0$}; 
       
  1259 \draw (1.5,-0.5) node {$Q_1$}; 
       
  1260 \draw (2.5,-0.5) node {$Q_2$}; 
       
  1261 \draw (3.5,-0.5) node {$Q_3$};
       
  1262  
       
  1263 \draw (-0.5, 3.5) node {$Q_1$}; 
       
  1264 \draw (-0.5, 2.5) node {$Q_2$}; 
       
  1265 \draw (-0.5, 1.5) node {$Q_3$}; 
       
  1266 \draw (-0.5, 0.5) node {$Q_4$}; 
       
  1267 
       
  1268 \draw (0.5,0.5) node {\large$\star$}; 
       
  1269 \draw (1.5,0.5) node {\large$\star$}; 
       
  1270 \draw (2.5,0.5) node {\large$\star$}; 
       
  1271 \draw (3.5,0.5) node {\large$\star$};
       
  1272 \end{tikzpicture}
       
  1273 \end{center}
       
  1274 
       
  1275 \noindent where the lower row is filled with stars, because in
       
  1276 the corresponding pairs there is always one state that is
       
  1277 accepting ($Q_4$) and a state that is non-accepting (the other
       
  1278 states).
       
  1279 
       
  1280 Now in Step 3 we need to fill in more stars according whether 
       
  1281 one of the next-state pairs are marked. We have to do this 
       
  1282 for every unmarked field until there is no change anymore.
       
  1283 This gives the triangle
       
  1284 
       
  1285 \begin{center}
       
  1286 \begin{tikzpicture}[scale=0.6,line width=0.8mm]
       
  1287 \draw (0,0) -- (4,0);
       
  1288 \draw (0,1) -- (4,1);
       
  1289 \draw (0,2) -- (3,2);
       
  1290 \draw (0,3) -- (2,3);
       
  1291 \draw (0,4) -- (1,4);
       
  1292 
       
  1293 \draw (0,0) -- (0, 4);
       
  1294 \draw (1,0) -- (1, 4);
       
  1295 \draw (2,0) -- (2, 3);
       
  1296 \draw (3,0) -- (3, 2);
       
  1297 \draw (4,0) -- (4, 1);
       
  1298 
       
  1299 \draw (0.5,-0.5) node {$Q_0$}; 
       
  1300 \draw (1.5,-0.5) node {$Q_1$}; 
       
  1301 \draw (2.5,-0.5) node {$Q_2$}; 
       
  1302 \draw (3.5,-0.5) node {$Q_3$};
       
  1303  
       
  1304 \draw (-0.5, 3.5) node {$Q_1$}; 
       
  1305 \draw (-0.5, 2.5) node {$Q_2$}; 
       
  1306 \draw (-0.5, 1.5) node {$Q_3$}; 
       
  1307 \draw (-0.5, 0.5) node {$Q_4$}; 
       
  1308 
       
  1309 \draw (0.5,0.5) node {\large$\star$}; 
       
  1310 \draw (1.5,0.5) node {\large$\star$}; 
       
  1311 \draw (2.5,0.5) node {\large$\star$}; 
       
  1312 \draw (3.5,0.5) node {\large$\star$};
       
  1313 \draw (0.5,1.5) node {\large$\star$}; 
       
  1314 \draw (2.5,1.5) node {\large$\star$}; 
       
  1315 \draw (0.5,3.5) node {\large$\star$}; 
       
  1316 \draw (1.5,2.5) node {\large$\star$}; 
       
  1317 \end{tikzpicture}
       
  1318 \end{center}
       
  1319 
       
  1320 \noindent which means states $Q_0$ and $Q_2$, as well as $Q_1$
       
  1321 and $Q_3$ can be merged. This gives the following minimal DFA
       
  1322 
       
  1323 \begin{center}
       
  1324 \begin{tikzpicture}[>=stealth',very thick,auto,
       
  1325                     every state/.style={minimum size=0pt,
       
  1326                     inner sep=2pt,draw=blue!50,very thick,
       
  1327                     fill=blue!20}]
       
  1328 \node[state,initial]  (Q_02)  {$Q_{0, 2}$};
       
  1329 \node[state] (Q_13) [right=of Q_02] {$Q_{1, 3}$};
       
  1330 \node[state, accepting] (Q_4) [right=of Q_13] 
       
  1331   {$Q_{4\phantom{,0}}$};
       
  1332 \path[->] (Q_02) edge [bend left] node [above]  {$a$} (Q_13);
       
  1333 \path[->] (Q_13) edge [bend left] node [below]  {$b$} (Q_02);
       
  1334 \path[->] (Q_02) edge [loop below] node  {$b$} ();
       
  1335 \path[->] (Q_13) edge node [above]  {$a$} (Q_4);
       
  1336 \path[->] (Q_4) edge [loop above] node  {$a, b$} ();
       
  1337 \end{tikzpicture}
       
  1338 \end{center}
       
  1339 
       
  1340 \subsubsection*{Regular Languages}
       
  1341 
  1350 
  1342 Given the constructions in the previous sections we obtain 
  1351 Given the constructions in the previous sections we obtain 
  1343 the following overall picture:
  1352 the following overall picture:
  1344 
  1353 
  1345 \begin{center}
  1354 \begin{center}
  1379 \noindent So for deciding whether a string is recognised by a
  1388 \noindent So for deciding whether a string is recognised by a
  1380 regular expression, we could use our algorithm based on
  1389 regular expression, we could use our algorithm based on
  1381 derivatives or NFAs or DFAs. But let us quickly look at what
  1390 derivatives or NFAs or DFAs. But let us quickly look at what
  1382 the differences mean in computational terms. Translating a
  1391 the differences mean in computational terms. Translating a
  1383 regular expression into a NFA gives us an automaton that has
  1392 regular expression into a NFA gives us an automaton that has
  1384 $O(n)$ nodes---that means the size of the NFA grows linearly
  1393 $O(n)$ states---that means the size of the NFA grows linearly
  1385 with the size of the regular expression. The problem with NFAs
  1394 with the size of the regular expression. The problem with NFAs
  1386 is that the problem of deciding whether a string is accepted
  1395 is that the problem of deciding whether a string is accepted
  1387 or not is computationally not cheap. Remember with NFAs we
  1396 or not is computationally not cheap. Remember with NFAs we
  1388 have potentially many next states even for the same input and
  1397 have potentially many next states even for the same input and
  1389 also have the silent $\epsilon$-transitions. If we want to
  1398 also have the silent $\epsilon$-transitions. If we want to
  1439 expression for this language and also not an automaton. One
  1448 expression for this language and also not an automaton. One
  1440 can actually prove that there is no regular expression nor
  1449 can actually prove that there is no regular expression nor
  1441 automaton for this language, but again that would lead us too
  1450 automaton for this language, but again that would lead us too
  1442 far afield for what we want to do in this module.
  1451 far afield for what we want to do in this module.
  1443 
  1452 
  1444 \section*{Further Reading}
  1453 %\section*{Further Reading}
  1445 
  1454 
  1446 Compare what a ``human expert'' would create as an automaton for the
  1455 %Compare what a ``human expert'' would create as an automaton for the
  1447 regular expression $a (b + c)^*$ and what the Thomson
  1456 %regular expression $a\cdot (b + c)^*$ and what the Thomson
  1448 algorithm generates.
  1457 %algorithm generates.
  1449 
  1458 
  1450 %http://www.inf.ed.ac.uk/teaching/courses/ct/
  1459 %http://www.inf.ed.ac.uk/teaching/courses/ct/
  1451 \end{document}
  1460 \end{document}
  1452 
  1461 
  1453 %%% Local Variables: 
  1462 %%% Local Variables: