| author | Christian Urban <urbanc@in.tum.de> | 
| Tue, 25 Apr 2017 12:28:07 +0100 | |
| changeset 485 | 21dec9df46ba | 
| parent 484 | 8182eb3278e0 | 
| child 487 | ffbc65112d48 | 
| permissions | -rw-r--r-- | 
| 140 
1be892087df2
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 1 | \documentclass{article}
 | 
| 251 
5b5a68df6d16
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
217diff
changeset | 2 | \usepackage{../style}
 | 
| 
5b5a68df6d16
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
217diff
changeset | 3 | \usepackage{../langs}
 | 
| 268 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 4 | \usepackage{../graphics}
 | 
| 140 
1be892087df2
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 5 | |
| 480 | 6 | %We can even allow ``silent | 
| 7 | %transitions'', also called epsilon-transitions. They allow us | |
| 8 | %to go from one state to the next without having a character | |
| 9 | %consumed. We label such silent transition with the letter | |
| 10 | %$\epsilon$. | |
| 11 | ||
| 140 
1be892087df2
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 12 | \begin{document}
 | 
| 480 | 13 | \fnote{\copyright{} Christian Urban, King's College London, 2014, 2015, 2016, 2017}
 | 
| 140 
1be892087df2
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 14 | |
| 482 | 15 | \section*{Handout 3 (Finite Automata)}
 | 
| 140 
1be892087df2
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 16 | |
| 480 | 17 | Every formal language and compiler course I know of bombards you first | 
| 18 | with automata and then to a much, much smaller extend with regular | |
| 19 | expressions. As you can see, this course is turned upside down: | |
| 20 | regular expressions come first. The reason is that regular expressions | |
| 21 | are easier to reason about and the notion of derivatives, although | |
| 22 | already quite old, only became more widely known rather | |
| 23 | recently. Still let us in this lecture have a closer look at automata | |
| 24 | and their relation to regular expressions. This will help us with | |
| 25 | understanding why the regular expression matchers in Python, Ruby and | |
| 485 | 26 | Java are so slow with certain regular expressions. On the way we will | 
| 27 | also see what are the limitations of regular expressions. | |
| 482 | 28 | |
| 29 | ||
| 30 | \subsection*{Deterministic Finite Automata}
 | |
| 31 | ||
| 485 | 32 | Lets start\ldots the central definition is:\medskip | 
| 142 
1aa28135a2da
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
141diff
changeset | 33 | |
| 268 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 34 | \noindent | 
| 251 
5b5a68df6d16
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
217diff
changeset | 35 | A \emph{deterministic finite automaton} (DFA), say $A$, is
 | 
| 484 | 36 | given by a five-tuple written ${\cal A}(\varSigma, Qs, Q_0, F, \delta)$ where
 | 
| 142 
1aa28135a2da
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
141diff
changeset | 37 | |
| 
1aa28135a2da
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
141diff
changeset | 38 | \begin{itemize}
 | 
| 484 | 39 | \item $\varSigma$ is an alphabet, | 
| 482 | 40 | \item $Qs$ is a finite set of states, | 
| 41 | \item $Q_0 \in Qs$ is the start state, | |
| 42 | \item $F \subseteq Qs$ are the accepting states, and | |
| 142 
1aa28135a2da
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
141diff
changeset | 43 | \item $\delta$ is the transition function. | 
| 
1aa28135a2da
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
141diff
changeset | 44 | \end{itemize}
 | 
| 
1aa28135a2da
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
141diff
changeset | 45 | |
| 482 | 46 | \noindent The transition function determines how to ``transition'' | 
| 47 | from one state to the next state with respect to a character. We have | |
| 48 | the assumption that these transition functions do not need to be | |
| 49 | defined everywhere: so it can be the case that given a character there | |
| 50 | is no next state, in which case we need to raise a kind of ``failure | |
| 483 | 51 | exception''.  That means actually we have \emph{partial} functions as
 | 
| 484 | 52 | transitions---see the Scala implementation of DFAs later on. A | 
| 53 | typical example of a DFA is | |
| 142 
1aa28135a2da
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
141diff
changeset | 54 | |
| 
1aa28135a2da
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
141diff
changeset | 55 | \begin{center}
 | 
| 
1aa28135a2da
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
141diff
changeset | 56 | \begin{tikzpicture}[>=stealth',very thick,auto,
 | 
| 268 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 57 |                     every state/.style={minimum size=0pt,
 | 
| 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 58 | inner sep=2pt,draw=blue!50,very thick, | 
| 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 59 | fill=blue!20},scale=2] | 
| 482 | 60 | \node[state,initial]  (Q_0)  {$Q_0$};
 | 
| 61 | \node[state] (Q_1) [right=of Q_0] {$Q_1$};
 | |
| 62 | \node[state] (Q_2) [below right=of Q_0] {$Q_2$};
 | |
| 63 | \node[state] (Q_3) [right=of Q_2] {$Q_3$};
 | |
| 64 | \node[state, accepting] (Q_4) [right=of Q_1] {$Q_4$};
 | |
| 65 | \path[->] (Q_0) edge node [above]  {$a$} (Q_1);
 | |
| 66 | \path[->] (Q_1) edge node [above]  {$a$} (Q_4);
 | |
| 67 | \path[->] (Q_4) edge [loop right] node  {$a, b$} ();
 | |
| 68 | \path[->] (Q_3) edge node [right]  {$a$} (Q_4);
 | |
| 69 | \path[->] (Q_2) edge node [above]  {$a$} (Q_3);
 | |
| 70 | \path[->] (Q_1) edge node [right]  {$b$} (Q_2);
 | |
| 71 | \path[->] (Q_0) edge node [above]  {$b$} (Q_2);
 | |
| 72 | \path[->] (Q_2) edge [loop left] node  {$b$} ();
 | |
| 73 | \path[->] (Q_3) edge [bend left=95, looseness=1.3] node [below]  {$b$} (Q_0);
 | |
| 142 
1aa28135a2da
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
141diff
changeset | 74 | \end{tikzpicture}
 | 
| 
1aa28135a2da
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
141diff
changeset | 75 | \end{center}
 | 
| 140 
1be892087df2
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 76 | |
| 482 | 77 | \noindent In this graphical notation, the accepting state $Q_4$ is | 
| 78 | indicated with double circles. Note that there can be more than one | |
| 79 | accepting state. It is also possible that a DFA has no accepting | |
| 485 | 80 | state at all, or that the starting state is also an accepting | 
| 482 | 81 | state. In the case above the transition function is defined everywhere | 
| 82 | and can also be given as a table as follows: | |
| 143 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 83 | |
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 84 | \[ | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 85 | \begin{array}{lcl}
 | 
| 482 | 86 | (Q_0, a) &\rightarrow& Q_1\\ | 
| 87 | (Q_0, b) &\rightarrow& Q_2\\ | |
| 88 | (Q_1, a) &\rightarrow& Q_4\\ | |
| 89 | (Q_1, b) &\rightarrow& Q_2\\ | |
| 90 | (Q_2, a) &\rightarrow& Q_3\\ | |
| 91 | (Q_2, b) &\rightarrow& Q_2\\ | |
| 92 | (Q_3, a) &\rightarrow& Q_4\\ | |
| 93 | (Q_3, b) &\rightarrow& Q_0\\ | |
| 94 | (Q_4, a) &\rightarrow& Q_4\\ | |
| 95 | (Q_4, b) &\rightarrow& Q_4\\ | |
| 143 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 96 | \end{array}
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 97 | \] | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 98 | |
| 268 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 99 | We need to define the notion of what language is accepted by | 
| 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 100 | an automaton. For this we lift the transition function | 
| 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 101 | $\delta$ from characters to strings as follows: | 
| 143 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 102 | |
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 103 | \[ | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 104 | \begin{array}{lcl}
 | 
| 484 | 105 | \widehat{\delta}(q, [])        & \dn & q\\
 | 
| 106 | \widehat{\delta}(q, c\!::\!s) & \dn & \widehat{\delta}(\delta(q, c), s)\\
 | |
| 143 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 107 | \end{array}
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 108 | \] | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 109 | |
| 268 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 110 | \noindent This lifted transition function is often called | 
| 485 | 111 | \emph{delta-hat}. Given a string, we start in the starting state and
 | 
| 112 | take the first character of the string, follow to the next state, then | |
| 113 | take the second character and so on. Once the string is exhausted and | |
| 114 | we end up in an accepting state, then this string is accepted by the | |
| 480 | 115 | automaton. Otherwise it is not accepted. This also means that if along | 
| 116 | the way we hit the case where the transition function $\delta$ is not | |
| 117 | defined, we need to raise an error. In our implementation we will deal | |
| 485 | 118 | with this case elegantly by using Scala's \texttt{Try}.  Summing up: a
 | 
| 119 | string $s$ is in the \emph{language accepted by the automaton} ${\cal
 | |
| 484 | 120 | A}(\varSigma, Q, Q_0, F, \delta)$ iff | 
| 143 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 121 | |
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 122 | \[ | 
| 484 | 123 | \widehat{\delta}(Q_0, s) \in F 
 | 
| 143 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 124 | \] | 
| 268 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 125 | |
| 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 126 | \noindent I let you think about a definition that describes | 
| 483 | 127 | the set of all strings accepted by an automaton. | 
| 480 | 128 | |
| 129 | \begin{figure}[p]
 | |
| 130 | \small | |
| 131 | \lstinputlisting[numbers=left,linebackgroundcolor= | |
| 132 |                   {\ifodd\value{lstnumber}\color{capri!3}\fi}]
 | |
| 133 |                   {../progs/dfa.scala}
 | |
| 482 | 134 | \caption{A Scala implementation of DFAs using partial functions.
 | 
| 480 | 135 |   Notice some subtleties: \texttt{deltas} implements the delta-hat
 | 
| 485 | 136 | construction by lifting the transition (partial) function to lists | 
| 137 |   of characters. Since \texttt{delta} is given as a partial function,
 | |
| 138 |   it can obviously go ``wrong'' in which case the \texttt{Try} in
 | |
| 139 |   \texttt{accepts} catches the error and returns \texttt{false}---that
 | |
| 140 |   means the string is not accepted.  The example \texttt{delta} in
 | |
| 141 | Line 28--38 implements the DFA example shown earlier in the | |
| 142 |   handout.\label{dfa}}
 | |
| 480 | 143 | \end{figure}
 | 
| 144 | ||
| 485 | 145 | My take on an implementation of DFAs in Scala is given in | 
| 483 | 146 | Figure~\ref{dfa}. As you can see, there are many features of the
 | 
| 482 | 147 | mathematical definition that are quite closely reflected in the | 
| 148 | code. In the DFA-class, there is a starting state, called | |
| 483 | 149 | \texttt{start}, with the polymorphic type \texttt{A}.  There is a
 | 
| 150 | partial function \texttt{delta} for specifying the transitions---these
 | |
| 151 | partial functions take a state (of polymorphic type \texttt{A}) and an
 | |
| 152 | input (of polymorphic type \texttt{C}) and produce a new state (of
 | |
| 153 | type \texttt{A}). For the moment it is OK to assume that \texttt{A} is
 | |
| 154 | some arbitrary type for states and the input is just characters. (The | |
| 485 | 155 | reason for not having concrete types, but polymorphic types for the | 
| 156 | states and the input of DFAs will become clearer later on.) | |
| 483 | 157 | |
| 485 | 158 | The DFA-class has also an argument for specifying final states. In the | 
| 159 | implementation it is not a set of states, as in the mathematical | |
| 160 | definition, but a function from states to booleans (this function is | |
| 161 | supposed to return true whenever a state is final; false | |
| 162 | otherwise). While this boolean function is different from the sets of | |
| 163 | states, Scala allows to use sets for such functions (see Line 40 where | |
| 164 | the DFA is initialised). Again it will become clear later on why I use | |
| 165 | functions for final states, rather than sets. | |
| 166 | ||
| 167 | The most important point in the implementation is that I use Scala's | |
| 484 | 168 | partial functions for representing the transitions; alternatives would | 
| 169 | have been \texttt{Maps} or even \texttt{Lists}. One of the main
 | |
| 170 | advantages of using partial functions is that transitions can be quite | |
| 171 | nicely defined by a series of \texttt{case} statements (see Lines 28
 | |
| 172 | -- 38 for an example). If you need to represent an automaton with a | |
| 173 | sink state (catch-all-state), you can use Scala's pattern matching and | |
| 174 | write something like | |
| 143 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 175 | |
| 480 | 176 | {\small\begin{lstlisting}[language=Scala,linebackgroundcolor=
 | 
| 177 |                   {\ifodd\value{lstnumber}\color{capri!3}\fi}]
 | |
| 178 | abstract class State | |
| 179 | ... | |
| 180 | case object Sink extends State | |
| 181 | ||
| 182 | val delta : (State, Char) :=> State = | |
| 183 |   { case (S0, 'a') => S1
 | |
| 184 | case (S1, 'a') => S2 | |
| 185 | case _ => Sink | |
| 186 | } | |
| 187 | \end{lstlisting}}  
 | |
| 188 | ||
| 485 | 189 | \noindent I let you think what the corresponding DFA looks like in the | 
| 190 | graph notation. | |
| 484 | 191 | |
| 485 | 192 | Finally, I let you ponder whether this is a good implementation of | 
| 193 | DFAs or not. In doing so I hope you notice that the $\varSigma$ and | |
| 194 | $Qs$ components (the alphabet and the set of finite states, | |
| 195 | respectively) are missing from the class definition. This means that | |
| 196 | the implementation allows you to do some fishy things you are not | |
| 197 | meant to do with DFAs. Which fishy things could that be? | |
| 480 | 198 | |
| 482 | 199 | |
| 200 | ||
| 201 | \subsection*{Non-Deterministic Finite Automata}
 | |
| 202 | ||
| 485 | 203 | Remember we want to find out what the relation is between regular | 
| 204 | expressions and automata. To do this with DFAs is a bit unwieldy. | |
| 205 | While with DFAs it is always clear that given a state and a character | |
| 206 | what the next state is (potentially none), it will be convenient to | |
| 207 | relax this restriction. That means we allow states to have several | |
| 482 | 208 | potential successor states. We even allow more than one starting | 
| 484 | 209 | state. The resulting construction is called a \emph{Non-Deterministic
 | 
| 485 | 210 |   Finite Automaton} (NFA) given also as a five-tuple ${\cal
 | 
| 211 |   A}(\varSigma, Qs, Q_{0s}, F, \rho)$ where
 | |
| 143 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 212 | |
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 213 | \begin{itemize}
 | 
| 484 | 214 | \item $\varSigma$ is an alphabet, | 
| 482 | 215 | \item $Qs$ is a finite set of states | 
| 216 | \item $Q_{0s}$ is a set of start states ($Q_{0s} \subseteq Qs$)
 | |
| 217 | \item $F$ are some accepting states with $F \subseteq Qs$, and | |
| 143 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 218 | \item $\rho$ is a transition relation. | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 219 | \end{itemize}
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 220 | |
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 221 | \noindent | 
| 483 | 222 | A typical example of a NFA is | 
| 482 | 223 | |
| 224 | % A NFA for (ab* + b)*a | |
| 143 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 225 | \begin{center}
 | 
| 483 | 226 | \begin{tikzpicture}[>=stealth',very thick, auto,
 | 
| 227 |     every state/.style={minimum size=0pt,inner sep=3pt,
 | |
| 228 | draw=blue!50,very thick,fill=blue!20},scale=2] | |
| 482 | 229 | \node[state,initial]  (Q_0)  {$Q_0$};
 | 
| 230 | \node[state] (Q_1) [right=of Q_0] {$Q_1$};
 | |
| 231 | \node[state, accepting] (Q_2) [right=of Q_1] {$Q_2$};
 | |
| 232 | \path[->] (Q_0) edge [loop above] node  {$b$} ();
 | |
| 233 | \path[<-] (Q_0) edge node [below]  {$b$} (Q_1);
 | |
| 234 | \path[->] (Q_0) edge [bend left] node [above]  {$a$} (Q_1);
 | |
| 235 | \path[->] (Q_0) edge [bend right] node [below]  {$a$} (Q_2);
 | |
| 236 | \path[->] (Q_1) edge [loop above] node  {$a,b$} ();
 | |
| 237 | \path[->] (Q_1) edge node  [above] {$a$} (Q_2);
 | |
| 238 | \end{tikzpicture}
 | |
| 143 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 239 | \end{center}
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 240 | |
| 482 | 241 | \noindent | 
| 242 | This NFA happens to have only one starting state, but in general there | |
| 243 | could be more. Notice that in state $Q_0$ we might go to state $Q_1$ | |
| 483 | 244 | \emph{or} to state $Q_2$ when receiving an $a$. Similarly in state
 | 
| 484 | 245 | $Q_1$ and receiving an $a$, we can stay in state $Q_1$ \emph{or} go to
 | 
| 246 | $Q_2$. This kind of choice is not allowed with DFAs. The downside of | |
| 247 | this choice is that when it comes to deciding whether a string is | |
| 248 | accepted by a NFA we potentially have to explore all possibilities. I | |
| 249 | let you think which kind of strings the above NFA accepts. | |
| 482 | 250 | |
| 251 | ||
| 485 | 252 | There are a number of additional points you should note about | 
| 483 | 253 | NFAs. Every DFA is a NFA, but not vice versa. The $\rho$ in NFAs is a | 
| 254 | transition \emph{relation} (DFAs have a transition function). The
 | |
| 255 | difference between a function and a relation is that a function has | |
| 256 | always a single output, while a relation gives, roughly speaking, | |
| 257 | several outputs. Look again at the NFA above: if you are currently in | |
| 258 | the state $Q_1$ and you read a character $b$, then you can transition | |
| 259 | to either $Q_0$ \emph{or} $Q_2$. Which route, or output, you take is
 | |
| 260 | not determined. This non-determinism can be represented by a | |
| 261 | relation. | |
| 482 | 262 | |
| 483 | 263 | My implementation of NFAs in Scala is shown in Figure~\ref{nfa}.
 | 
| 264 | Perhaps interestingly, I do not actually use relations for my NFAs, | |
| 485 | 265 | but use transition functions that return sets of states. DFAs have | 
| 266 | partial transition functions that return a single state; my NFAs | |
| 267 | return a set. I let you think about this representation for | |
| 268 | NFA-transitions and how it corresponds to the relations used in the | |
| 269 | mathematical definition of NFAs. | |
| 482 | 270 | |
| 485 | 271 | Like in the mathematical definition, \texttt{starts} is in NFAs a set
 | 
| 272 | of states; \texttt{fins} is again a function from states to
 | |
| 273 | booleans. The \texttt{next} function calculates the set of next states
 | |
| 274 | reachable from a single state \texttt{q} by a character~\texttt{c}. In
 | |
| 275 | case there is no such state---the partial transition function is | |
| 276 | undefined---the empty set is returned (see function | |
| 277 | \texttt{applyOrElse} in Lines 9 and 10). The function \texttt{nexts}
 | |
| 278 | just lifts this function to sets of states. | |
| 279 | ||
| 484 | 280 | \begin{figure}[p]
 | 
| 482 | 281 | \small | 
| 282 | \lstinputlisting[numbers=left,linebackgroundcolor= | |
| 283 |                   {\ifodd\value{lstnumber}\color{capri!3}\fi}]
 | |
| 284 |                   {../progs/nfa.scala}
 | |
| 485 | 285 | \caption{A Scala implementation of NFAs using partial functions.
 | 
| 286 |   Notice that the function \texttt{accepts} implements the
 | |
| 287 | acceptance of a string in a breath-first search fashion. This can be a costly | |
| 288 | way of deciding whether a string is accepted or not in applications that need to handle | |
| 289 |   large NFAs and large inputs.\label{nfa}}
 | |
| 482 | 290 | \end{figure}
 | 
| 291 | ||
| 485 | 292 | Look very careful at the \texttt{accepts} and \texttt{deltas}
 | 
| 293 | functions in NFAs and remember that when accepting a string by a NFA | |
| 484 | 294 | we might have to explore all possible transitions (recall which state | 
| 485 | 295 | to go to is not unique anymore with NFAs\ldots{}we need to explore
 | 
| 296 | potentially all next states). The implementation achieves this | |
| 297 | exploration in a \emph{breadth-first search} manner. This is fine for
 | |
| 298 | small NFAs, but can lead to real memory problems when the NFAs are | |
| 299 | bigger and larger strings need to be processed. As result, some | |
| 300 | regular expression matching engines resort to a \emph{depth-first
 | |
| 301 |   search} with \emph{backtracking} in unsuccessful cases. In our
 | |
| 302 | implementation we can implement a depth-first version of | |
| 303 | \texttt{accepts} using Scala's \texttt{exists}-function as follows:
 | |
| 483 | 304 | |
| 305 | ||
| 306 | {\small\begin{lstlisting}[language=Scala,linebackgroundcolor=
 | |
| 307 |                   {\ifodd\value{lstnumber}\color{capri!3}\fi}]
 | |
| 308 | def search(q: A, s: List[C]) : Boolean = s match {
 | |
| 309 | case Nil => fins(q) | |
| 485 | 310 | case c::cs => next(q, c).exists(search(_, cs)) | 
| 483 | 311 | } | 
| 312 | ||
| 485 | 313 | def accepts2(s: List[C]) : Boolean = | 
| 483 | 314 | starts.exists(search(_, s)) | 
| 315 | \end{lstlisting}}
 | |
| 316 | ||
| 317 | \noindent | |
| 318 | This depth-first way of exploration seems to work efficiently in many | |
| 485 | 319 | examples and is much less of a strain on memory. The problem is that | 
| 320 | the backtracking can get ``catastrophic'' in some examples---remember | |
| 321 | the catastrophic backtracking from earlier lectures. This depth-first | |
| 483 | 322 | search with backtracking is the reason for the abysmal performance of | 
| 485 | 323 | some regular expression matchings in Java, Ruby and Python. I like to | 
| 484 | 324 | show you this next. | 
| 482 | 325 | |
| 268 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 326 | |
| 269 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 327 | \subsubsection*{Thompson Construction}
 | 
| 143 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 328 | |
| 485 | 329 | In order to get an idea what calculations are performed by Java \& | 
| 330 | friends, we need a method for transforming a regular expression into | |
| 331 | an automaton. This automaton should accept exactly those strings that | |
| 332 | are accepted by the regular expression. The simplest and most | |
| 333 | well-known method for this is called \emph{Thompson Construction},
 | |
| 334 | after the Turing Award winner Ken Thompson. This method is by | |
| 335 | recursion over regular expressions and uses the non-determinism in | |
| 336 | NFAs. You will see shortly why this construction works well with NFAs, | |
| 337 | but is not so straightforward with DFAs. Unfortunately we are still | |
| 338 | one step away from our intended target though---because this | |
| 339 | construction uses a version of NFAs that allows ``silent | |
| 340 | transitions''. The idea behind silent transitions is that they | |
| 341 | allow us to go from one state to the next without having to consume a | |
| 342 | character. We label such silent transition with the letter | |
| 343 | $\epsilon$ and call the automata $\epsilon$NFAs. Two | |
| 344 | typical examples of $\epsilon$NFAs are | |
| 484 | 345 | |
| 346 | ||
| 485 | 347 | \begin{center}
 | 
| 348 | \begin{tabular}[t]{c@{\hspace{9mm}}c}
 | |
| 349 | \begin{tikzpicture}[>=stealth',very thick,
 | |
| 350 |     every state/.style={minimum size=0pt,draw=blue!50,very thick,fill=blue!20},]
 | |
| 351 | \node[state,initial]  (Q_0)  {$Q_0$};
 | |
| 352 | \node[state] (Q_1) [above=of Q_0] {$Q_1$};
 | |
| 353 | \node[state, accepting] (Q_2) [below=of Q_0] {$Q_2$};
 | |
| 354 | \path[->] (Q_0) edge node [left]  {$\epsilon$} (Q_1);
 | |
| 355 | \path[->] (Q_0) edge node [left]  {$\epsilon$} (Q_2);
 | |
| 356 | \path[->] (Q_0) edge [loop right] node  {$a$} ();
 | |
| 357 | \path[->] (Q_1) edge [loop right] node  {$a$} ();
 | |
| 358 | \path[->] (Q_2) edge [loop right] node  {$b$} ();
 | |
| 359 | \end{tikzpicture} &
 | |
| 360 | ||
| 361 | \raisebox{20mm}{
 | |
| 362 | \begin{tikzpicture}[>=stealth',very thick,
 | |
| 363 |     every state/.style={minimum size=0pt,draw=blue!50,very thick,fill=blue!20},]
 | |
| 364 | \node[state,initial]  (r_1)  {$R_1$};
 | |
| 365 | \node[state] (r_2) [above=of r_1] {$R_2$};
 | |
| 366 | \node[state, accepting] (r_3) [right=of r_1] {$R_3$};
 | |
| 367 | \path[->] (r_1) edge node [below]  {$b$} (r_3);
 | |
| 368 | \path[->] (r_2) edge [bend left] node [above]  {$a$} (r_3);
 | |
| 369 | \path[->] (r_1) edge [bend left] node  [left] {$\epsilon$} (r_2);
 | |
| 370 | \path[->] (r_2) edge [bend left] node  [right] {$a$} (r_1);
 | |
| 371 | \end{tikzpicture}}
 | |
| 372 | \end{tabular}
 | |
| 373 | \end{center}
 | |
| 374 | ||
| 375 | \noindent | |
| 376 | Consider the $\epsilon$NFA on the left-hand side: an | |
| 377 | $\epsilon$-transition means you do not have to ``consume'' any part of | |
| 378 | the input string, but ``silently'' change to a different state. For | |
| 379 | example, if you are in the starting state $Q_0$, you can silently move | |
| 380 | either to $Q_1$ or $Q_2$. In this example you can see that once you | |
| 381 | are in $Q_1$, respectively $Q_2$, you cannot ``go back'' to the other | |
| 382 | states. | |
| 383 | ||
| 384 | On first appearances, $\epsilon$-transitions might look rather | |
| 385 | strange, or even silly. To start with, silent transitions make the | |
| 386 | decision whether a string is accepted by an automaton even harder: | |
| 387 | with $\epsilon$NFAs we have to look whether we can do first some | |
| 388 | $\epsilon$-transitions and then do a ``proper''-transition; and after | |
| 389 | any ``proper''-transition we again have to check whether we can do | |
| 390 | again some silent transitions. Even worse, if there is a silent | |
| 391 | transition pointing back to the same state, then we have to be careful | |
| 392 | our decision procedure for strings does not loop (remember the | |
| 393 | depth-first search for exploring all states). | |
| 394 | ||
| 395 | The obvious question is: Do we get anything in return for this hassle | |
| 396 | with silent transitions? Well, we still have to work for it\ldots | |
| 397 | unfortunately. If we were to follow the many textbooks on the | |
| 398 | subject, we would now start with defining what $\epsilon$NFAs | |
| 399 | are---that would require extending the transition relation of | |
| 400 | NFAs. Next, show that the $\epsilon$NFAs are equivalent to NFAs and so | |
| 401 | on. Once we have done all this on paper, we would need to implement | |
| 402 | $\epsilon$NFAs. Lets try to take a shortcut---we are not really | |
| 403 | interested in $\epsilon$NFAs; they are only a convenient tool for | |
| 404 | translating regular expressions into automata. We are not going to | |
| 405 | implementing them explicitly, but translate them directly into NFAs | |
| 406 | (in a sense $\epsilon$NFAs are just a convenient API for lazy people). | |
| 407 | How does the translation work: well we have to find all transitions of | |
| 408 | the form | |
| 409 | ||
| 410 | \[ | |
| 411 | q\stackrel{\epsilon}{\longrightarrow}\ldots\stackrel{\epsilon}{\longrightarrow}
 | |
| 412 | \;\stackrel{a}{\longrightarrow}\;
 | |
| 413 | \stackrel{\epsilon}{\longrightarrow}\ldots\stackrel{\epsilon}{\longrightarrow} q'
 | |
| 414 | \] | |
| 415 | ||
| 416 | \noindent and replace them with $q \stackrel{a}{\longrightarrow}
 | |
| 417 | q'$. Doing this to the $\epsilon$NFA on the left-hand side above gives | |
| 418 | the NFA | |
| 419 | ||
| 420 | \begin{center}
 | |
| 421 | \begin{tikzpicture}[>=stealth',very thick,
 | |
| 422 |     every state/.style={minimum size=0pt,draw=blue!50,very thick,fill=blue!20},]
 | |
| 423 | \node[state,initial]  (r_1)  {$R_1$};
 | |
| 424 | \node[state] (r_2) [above=of r_1] {$R_2$};
 | |
| 425 | \node[state, accepting] (r_3) [right=of r_1] {$R_3$};
 | |
| 426 | \path[->] (r_1) edge node [above]  {$b$} (r_3);
 | |
| 427 | \path[->] (r_2) edge [bend left] node [above]  {$a$} (r_3);
 | |
| 428 | \path[->] (r_1) edge [bend left] node  [left] {$a$} (r_2);
 | |
| 429 | \path[->] (r_2) edge [bend left] node  [right] {$a$} (r_1);
 | |
| 430 | \path[->] (r_1) edge [loop below] node  {$a$} ();
 | |
| 431 | \path[->] (r_1) edge [bend right] node [below]  {$a$} (r_3);
 | |
| 432 | \end{tikzpicture}
 | |
| 433 | \end{center}
 | |
| 434 | ||
| 435 | \noindent where the single the $\epsilon$-transition is replaced by | |
| 436 | three more $a$-transitions. So whenever we are given $\epsilon$NFA we | |
| 437 | will, similarly, replace it by an equivalent NFA. The code for this is | |
| 438 | given in Figure~\ref{enfa}. At the core is a function that calculates
 | |
| 439 | a fixpoint of function (Lines 5--10). This is used for ``discovering'' | |
| 440 | states reachable by $\epsilon$-transitions. Once no new state is | |
| 441 | reachable state, a fixpoint is reached. This is used for example when | |
| 442 | calculating the starting states of an equivalent NFA---see Line 36. | |
| 443 | We starts with all starting states of the $\epsilon$NFA and then look | |
| 444 | for all other states that can be reached by | |
| 445 | $\epsilon$-transition. This is what the $\epsilon$-closure, called | |
| 446 | \texttt{ecl}, calculates.
 | |
| 447 | ||
| 448 | \begin{figure}[p]
 | |
| 449 | \small | |
| 450 | \lstinputlisting[numbers=left,linebackgroundcolor= | |
| 451 |                   {\ifodd\value{lstnumber}\color{capri!3}\fi}]
 | |
| 452 |                   {../progs/enfa.scala}
 | |
| 453 | ||
| 454 | \caption{A Scala function that translates $\epsilon$NFA into NFAs. The
 | |
| 455 |   transtions of $\epsilon$NFA take as input an \texttt{Option[C]}.
 | |
| 456 |   \texttt{None} stands for an $\epsilon$-transition; \texttt{Some(c)}
 | |
| 457 | for a ``proper'' transition. The functions in Lines 18--26 calculate | |
| 458 | all states reachable by one or more $\epsilon$-transition for a given | |
| 459 |   set of states. The NFA is constructed in in Lines 36--38.\label{enfa}}
 | |
| 460 | \end{figure}
 | |
| 461 | ||
| 462 | ||
| 463 | Now having a translation of $\epsilon$NFAs to NFAs in place, we can | |
| 464 | finally make headway with the problem of translating regular | |
| 465 | expressions into equivalent NFAs. By equivalent we mean that the NFAs | |
| 466 | recognise the same language. Consider the simple regular expressions | |
| 467 | $\ZERO$, $\ONE$ and $c$. They can be translated into equivalent NFAs | |
| 468 | as follows: | |
| 143 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 469 | |
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 470 | \begin{center}
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 471 | \begin{tabular}[t]{l@{\hspace{10mm}}l}
 | 
| 444 
3056a4c071b0
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
349diff
changeset | 472 | \raisebox{1mm}{$\ZERO$} & 
 | 
| 143 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 473 | \begin{tikzpicture}[scale=0.7,>=stealth',very thick, every state/.style={minimum size=3pt,draw=blue!50,very thick,fill=blue!20},]
 | 
| 482 | 474 | \node[state, initial]  (Q_0)  {$\mbox{}$};
 | 
| 143 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 475 | \end{tikzpicture}\\\\
 | 
| 444 
3056a4c071b0
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
349diff
changeset | 476 | \raisebox{1mm}{$\ONE$} & 
 | 
| 143 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 477 | \begin{tikzpicture}[scale=0.7,>=stealth',very thick, every state/.style={minimum size=3pt,draw=blue!50,very thick,fill=blue!20},]
 | 
| 482 | 478 | \node[state, initial, accepting]  (Q_0)  {$\mbox{}$};
 | 
| 143 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 479 | \end{tikzpicture}\\\\
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 480 | \raisebox{2mm}{$c$} & 
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 481 | \begin{tikzpicture}[scale=0.7,>=stealth',very thick, every state/.style={minimum size=3pt,draw=blue!50,very thick,fill=blue!20},]
 | 
| 482 | 482 | \node[state, initial]  (Q_0)  {$\mbox{}$};
 | 
| 483 | \node[state, accepting]  (Q_1)  [right=of Q_0] {$\mbox{}$};
 | |
| 484 | \path[->] (Q_0) edge node [below]  {$c$} (Q_1);
 | |
| 143 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 485 | \end{tikzpicture}\\\\
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 486 | \end{tabular}
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 487 | \end{center}
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 488 | |
| 485 | 489 | \begin{figure}[p]
 | 
| 490 | \small | |
| 491 | \lstinputlisting[numbers=left,linebackgroundcolor= | |
| 492 |                   {\ifodd\value{lstnumber}\color{capri!3}\fi}]
 | |
| 493 |                   {../progs/thompson.scala}
 | |
| 494 | \caption{A Scala XXX\label{thompson}}
 | |
| 495 | \end{figure}
 | |
| 496 | ||
| 497 | ||
| 251 
5b5a68df6d16
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
217diff
changeset | 498 | \noindent The case for the sequence regular expression $r_1 | 
| 
5b5a68df6d16
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
217diff
changeset | 499 | \cdot r_2$ is as follows: We are given by recursion two | 
| 
5b5a68df6d16
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
217diff
changeset | 500 | automata representing $r_1$ and $r_2$ respectively. | 
| 143 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 501 | |
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 502 | \begin{center}
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 503 | \begin{tikzpicture}[node distance=3mm,
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 504 |                              >=stealth',very thick, every state/.style={minimum size=3pt,draw=blue!50,very thick,fill=blue!20},]
 | 
| 482 | 505 | \node[state, initial]  (Q_0)  {$\mbox{}$};
 | 
| 506 | \node (r_1)  [right=of Q_0] {$\ldots$};
 | |
| 143 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 507 | \node[state, accepting]  (t_1)  [right=of r_1] {$\mbox{}$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 508 | \node[state, accepting]  (t_2)  [above=of t_1] {$\mbox{}$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 509 | \node[state, accepting]  (t_3)  [below=of t_1] {$\mbox{}$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 510 | \node[state, initial]  (a_0)  [right=2.5cm of t_1] {$\mbox{}$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 511 | \node (b_1)  [right=of a_0] {$\ldots$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 512 | \node[state, accepting]  (c_1)  [right=of b_1] {$\mbox{}$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 513 | \node[state, accepting]  (c_2)  [above=of c_1] {$\mbox{}$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 514 | \node[state, accepting]  (c_3)  [below=of c_1] {$\mbox{}$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 515 | \begin{pgfonlayer}{background}
 | 
| 482 | 516 | \node (1) [rounded corners, inner sep=1mm, thick, draw=black!60, fill=black!20, fit= (Q_0) (r_1) (t_1) (t_2) (t_3)] {};
 | 
| 143 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 517 | \node (2) [rounded corners, inner sep=1mm, thick, draw=black!60, fill=black!20, fit= (a_0) (b_1) (c_1) (c_2) (c_3)] {};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 518 | \node [yshift=2mm] at (1.north) {$r_1$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 519 | \node [yshift=2mm] at (2.north) {$r_2$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 520 | \end{pgfonlayer}
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 521 | \end{tikzpicture}
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 522 | \end{center}
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 523 | |
| 251 
5b5a68df6d16
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
217diff
changeset | 524 | \noindent The first automaton has some accepting states. We | 
| 
5b5a68df6d16
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
217diff
changeset | 525 | obtain an automaton for $r_1\cdot r_2$ by connecting these | 
| 
5b5a68df6d16
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
217diff
changeset | 526 | accepting states with $\epsilon$-transitions to the starting | 
| 
5b5a68df6d16
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
217diff
changeset | 527 | state of the second automaton. By doing so we make them | 
| 
5b5a68df6d16
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
217diff
changeset | 528 | non-accepting like so: | 
| 143 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 529 | |
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 530 | \begin{center}
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 531 | \begin{tikzpicture}[node distance=3mm,
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 532 |                              >=stealth',very thick, every state/.style={minimum size=3pt,draw=blue!50,very thick,fill=blue!20},]
 | 
| 482 | 533 | \node[state, initial]  (Q_0)  {$\mbox{}$};
 | 
| 534 | \node (r_1)  [right=of Q_0] {$\ldots$};
 | |
| 143 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 535 | \node[state]  (t_1)  [right=of r_1] {$\mbox{}$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 536 | \node[state]  (t_2)  [above=of t_1] {$\mbox{}$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 537 | \node[state]  (t_3)  [below=of t_1] {$\mbox{}$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 538 | \node[state]  (a_0)  [right=2.5cm of t_1] {$\mbox{}$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 539 | \node (b_1)  [right=of a_0] {$\ldots$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 540 | \node[state, accepting]  (c_1)  [right=of b_1] {$\mbox{}$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 541 | \node[state, accepting]  (c_2)  [above=of c_1] {$\mbox{}$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 542 | \node[state, accepting]  (c_3)  [below=of c_1] {$\mbox{}$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 543 | \path[->] (t_1) edge node [above, pos=0.3]  {$\epsilon$} (a_0);
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 544 | \path[->] (t_2) edge node [above]  {$\epsilon$} (a_0);
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 545 | \path[->] (t_3) edge node [below]  {$\epsilon$} (a_0);
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 546 | |
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 547 | \begin{pgfonlayer}{background}
 | 
| 482 | 548 | \node (3) [rounded corners, inner sep=1mm, thick, draw=black!60, fill=black!20, fit= (Q_0) (c_1) (c_2) (c_3)] {};
 | 
| 143 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 549 | \node [yshift=2mm] at (3.north) {$r_1\cdot r_2$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 550 | \end{pgfonlayer}
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 551 | \end{tikzpicture}
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 552 | \end{center}
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 553 | |
| 251 
5b5a68df6d16
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
217diff
changeset | 554 | \noindent The case for the choice regular expression $r_1 + | 
| 
5b5a68df6d16
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
217diff
changeset | 555 | r_2$ is slightly different: We are given by recursion two | 
| 
5b5a68df6d16
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
217diff
changeset | 556 | automata representing $r_1$ and $r_2$ respectively. | 
| 143 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 557 | |
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 558 | \begin{center}
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 559 | \begin{tikzpicture}[node distance=3mm,
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 560 |                              >=stealth',very thick, every state/.style={minimum size=3pt,draw=blue!50,very thick,fill=blue!20},]
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 561 | \node at (0,0)  (1)  {$\mbox{}$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 562 | \node[state, initial]  (2)  [above right=16mm of 1] {$\mbox{}$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 563 | \node[state, initial]  (3)  [below right=16mm of 1] {$\mbox{}$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 564 | |
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 565 | \node (a)  [right=of 2] {$\ldots$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 566 | \node[state, accepting]  (a1)  [right=of a] {$\mbox{}$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 567 | \node[state, accepting]  (a2)  [above=of a1] {$\mbox{}$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 568 | \node[state, accepting]  (a3)  [below=of a1] {$\mbox{}$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 569 | |
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 570 | \node (b)  [right=of 3] {$\ldots$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 571 | \node[state, accepting]  (b1)  [right=of b] {$\mbox{}$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 572 | \node[state, accepting]  (b2)  [above=of b1] {$\mbox{}$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 573 | \node[state, accepting]  (b3)  [below=of b1] {$\mbox{}$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 574 | \begin{pgfonlayer}{background}
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 575 | \node (1) [rounded corners, inner sep=1mm, thick, draw=black!60, fill=black!20, fit= (2) (a1) (a2) (a3)] {};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 576 | \node (2) [rounded corners, inner sep=1mm, thick, draw=black!60, fill=black!20, fit= (3) (b1) (b2) (b3)] {};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 577 | \node [yshift=3mm] at (1.north) {$r_1$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 578 | \node [yshift=3mm] at (2.north) {$r_2$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 579 | \end{pgfonlayer}
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 580 | \end{tikzpicture}
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 581 | \end{center}
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 582 | |
| 251 
5b5a68df6d16
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
217diff
changeset | 583 | \noindent Each automaton has a single start state and | 
| 
5b5a68df6d16
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
217diff
changeset | 584 | potentially several accepting states. We obtain a NFA for the | 
| 
5b5a68df6d16
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
217diff
changeset | 585 | regular expression $r_1 + r_2$ by introducing a new starting | 
| 
5b5a68df6d16
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
217diff
changeset | 586 | state and connecting it with an $\epsilon$-transition to the | 
| 
5b5a68df6d16
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
217diff
changeset | 587 | two starting states above, like so | 
| 143 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 588 | |
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 589 | \begin{center}
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 590 | \hspace{2cm}\begin{tikzpicture}[node distance=3mm,
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 591 |                              >=stealth',very thick, every state/.style={minimum size=3pt,draw=blue!50,very thick,fill=blue!20},]
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 592 | \node at (0,0) [state, initial]  (1)  {$\mbox{}$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 593 | \node[state]  (2)  [above right=16mm of 1] {$\mbox{}$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 594 | \node[state]  (3)  [below right=16mm of 1] {$\mbox{}$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 595 | |
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 596 | \node (a)  [right=of 2] {$\ldots$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 597 | \node[state, accepting]  (a1)  [right=of a] {$\mbox{}$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 598 | \node[state, accepting]  (a2)  [above=of a1] {$\mbox{}$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 599 | \node[state, accepting]  (a3)  [below=of a1] {$\mbox{}$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 600 | |
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 601 | \node (b)  [right=of 3] {$\ldots$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 602 | \node[state, accepting]  (b1)  [right=of b] {$\mbox{}$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 603 | \node[state, accepting]  (b2)  [above=of b1] {$\mbox{}$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 604 | \node[state, accepting]  (b3)  [below=of b1] {$\mbox{}$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 605 | |
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 606 | \path[->] (1) edge node [above]  {$\epsilon$} (2);
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 607 | \path[->] (1) edge node [below]  {$\epsilon$} (3);
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 608 | |
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 609 | \begin{pgfonlayer}{background}
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 610 | \node (3) [rounded corners, inner sep=1mm, thick, draw=black!60, fill=black!20, fit= (1) (a2) (a3) (b2) (b3)] {};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 611 | \node [yshift=3mm] at (3.north) {$r_1+ r_2$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 612 | \end{pgfonlayer}
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 613 | \end{tikzpicture}
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 614 | \end{center}
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 615 | |
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 616 | \noindent | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 617 | Finally for the $*$-case we have an automaton for $r$ | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 618 | |
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 619 | \begin{center}
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 620 | \begin{tikzpicture}[node distance=3mm,
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 621 |                              >=stealth',very thick, every state/.style={minimum size=3pt,draw=blue!50,very thick,fill=blue!20},]
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 622 | \node at (0,0)  (1)  {$\mbox{}$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 623 | \node[state, initial]  (2)  [right=16mm of 1] {$\mbox{}$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 624 | \node (a)  [right=of 2] {$\ldots$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 625 | \node[state, accepting]  (a1)  [right=of a] {$\mbox{}$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 626 | \node[state, accepting]  (a2)  [above=of a1] {$\mbox{}$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 627 | \node[state, accepting]  (a3)  [below=of a1] {$\mbox{}$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 628 | \begin{pgfonlayer}{background}
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 629 | \node (1) [rounded corners, inner sep=1mm, thick, draw=black!60, fill=black!20, fit= (2) (a1) (a2) (a3)] {};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 630 | \node [yshift=3mm] at (1.north) {$r$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 631 | \end{pgfonlayer}
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 632 | \end{tikzpicture}
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 633 | \end{center}
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 634 | |
| 251 
5b5a68df6d16
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
217diff
changeset | 635 | \noindent and connect its accepting states to a new starting | 
| 
5b5a68df6d16
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
217diff
changeset | 636 | state via $\epsilon$-transitions. This new starting state is | 
| 268 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 637 | also an accepting state, because $r^*$ can recognise the | 
| 251 
5b5a68df6d16
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
217diff
changeset | 638 | empty string. This gives the following automaton for $r^*$: | 
| 143 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 639 | |
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 640 | \begin{center}
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 641 | \begin{tikzpicture}[node distance=3mm,
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 642 |                              >=stealth',very thick, every state/.style={minimum size=3pt,draw=blue!50,very thick,fill=blue!20},]
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 643 | \node at (0,0) [state, initial,accepting]  (1)  {$\mbox{}$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 644 | \node[state]  (2)  [right=16mm of 1] {$\mbox{}$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 645 | \node (a)  [right=of 2] {$\ldots$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 646 | \node[state]  (a1)  [right=of a] {$\mbox{}$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 647 | \node[state]  (a2)  [above=of a1] {$\mbox{}$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 648 | \node[state]  (a3)  [below=of a1] {$\mbox{}$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 649 | \path[->] (1) edge node [above]  {$\epsilon$} (2);
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 650 | \path[->] (a1) edge [bend left=45] node [above]  {$\epsilon$} (1);
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 651 | \path[->] (a2) edge [bend right] node [below]  {$\epsilon$} (1);
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 652 | \path[->] (a3) edge [bend left=45] node [below]  {$\epsilon$} (1);
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 653 | \begin{pgfonlayer}{background}
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 654 | \node (2) [rounded corners, inner sep=1mm, thick, draw=black!60, fill=black!20, fit= (1) (a2) (a3)] {};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 655 | \node [yshift=3mm] at (2.north) {$r^*$};
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 656 | \end{pgfonlayer}
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 657 | \end{tikzpicture}
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 658 | \end{center}
 | 
| 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 659 | |
| 251 
5b5a68df6d16
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
217diff
changeset | 660 | \noindent This construction of a NFA from a regular expression | 
| 
5b5a68df6d16
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
217diff
changeset | 661 | was invented by Ken Thompson in 1968. | 
| 143 
e3fd4c5995ef
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
142diff
changeset | 662 | |
| 268 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 663 | |
| 269 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 664 | \subsubsection*{Subset Construction}
 | 
| 268 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 665 | |
| 292 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 666 | What is interesting is that for every NFA we can find a DFA | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 667 | which recognises the same language. This can, for example, be | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 668 | done by the \emph{subset construction}. Consider again the NFA
 | 
| 485 | 669 | below on the left, consisting of nodes labelled $0$, $1$ and $2$. | 
| 268 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 670 | |
| 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 671 | \begin{center}
 | 
| 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 672 | \begin{tabular}{c@{\hspace{10mm}}c}
 | 
| 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 673 | \begin{tikzpicture}[scale=0.7,>=stealth',very thick,
 | 
| 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 674 |                     every state/.style={minimum size=0pt,
 | 
| 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 675 | draw=blue!50,very thick,fill=blue!20}, | 
| 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 676 | baseline=0mm] | 
| 482 | 677 | \node[state,initial]  (Q_0)  {$0$};
 | 
| 678 | \node[state] (Q_1) [above=of Q_0] {$1$};
 | |
| 679 | \node[state, accepting] (Q_2) [below=of Q_0] {$2$};
 | |
| 680 | \path[->] (Q_0) edge node [left]  {$\epsilon$} (Q_1);
 | |
| 681 | \path[->] (Q_0) edge node [left]  {$\epsilon$} (Q_2);
 | |
| 682 | \path[->] (Q_0) edge [loop right] node  {$a$} ();
 | |
| 683 | \path[->] (Q_1) edge [loop above] node  {$a$} ();
 | |
| 684 | \path[->] (Q_2) edge [loop below] node  {$b$} ();
 | |
| 268 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 685 | \end{tikzpicture}
 | 
| 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 686 | & | 
| 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 687 | \begin{tabular}{r|cl}
 | 
| 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 688 | nodes & $a$ & $b$\\ | 
| 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 689 | \hline | 
| 344 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 690 | $\{\}\phantom{\star}$ & $\{\}$ & $\{\}$\\
 | 
| 268 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 691 | $\{0\}\phantom{\star}$       & $\{0,1,2\}$   & $\{2\}$\\
 | 
| 344 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 692 | $\{1\}\phantom{\star}$       & $\{1\}$       & $\{\}$\\
 | 
| 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 693 | $\{2\}\star$  & $\{\}$ & $\{2\}$\\
 | 
| 268 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 694 | $\{0,1\}\phantom{\star}$     & $\{0,1,2\}$   & $\{2\}$\\
 | 
| 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 695 | $\{0,2\}\star$ & $\{0,1,2\}$   & $\{2\}$\\
 | 
| 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 696 | $\{1,2\}\star$ & $\{1\}$       & $\{2\}$\\
 | 
| 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 697 | s: $\{0,1,2\}\star$ & $\{0,1,2\}$ & $\{2\}$\\
 | 
| 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 698 | \end{tabular}
 | 
| 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 699 | \end{tabular}
 | 
| 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 700 | \end{center}
 | 
| 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 701 | |
| 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 702 | \noindent The nodes of the DFA are given by calculating all | 
| 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 703 | subsets of the set of nodes of the NFA (seen in the nodes | 
| 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 704 | column on the right). The table shows the transition function | 
| 344 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 705 | for the DFA. The first row states that $\{\}$ is the
 | 
| 268 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 706 | sink node which has transitions for $a$ and $b$ to itself. | 
| 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 707 | The next three lines are calculated as follows: | 
| 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 708 | |
| 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 709 | \begin{itemize}
 | 
| 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 710 | \item suppose you calculate the entry for the transition for | 
| 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 711 |       $a$ and the node $\{0\}$
 | 
| 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 712 | \item start from the node $0$ in the NFA | 
| 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 713 | \item do as many $\epsilon$-transition as you can obtaining a | 
| 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 714 |       set of nodes, in this case $\{0,1,2\}$
 | 
| 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 715 | \item filter out all notes that do not allow an $a$-transition | 
| 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 716 | from this set, this excludes $2$ which does not permit a | 
| 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 717 | $a$-transition | 
| 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 718 | \item from the remaining set, do as many $\epsilon$-transition | 
| 344 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 719 |       as you can, this yields again $\{0,1,2\}$     
 | 
| 268 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 720 | \item the resulting set specifies the transition from $\{0\}$
 | 
| 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 721 | when given an $a$ | 
| 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 722 | \end{itemize}
 | 
| 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 723 | |
| 344 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 724 | \noindent So the transition from the state $\{0\}$ reading an
 | 
| 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 725 | $a$ goes to the state $\{0,1,2\}$. Similarly for the other
 | 
| 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 726 | entries in the rows for $\{0\}$, $\{1\}$ and $\{2\}$. The
 | 
| 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 727 | other rows are calculated by just taking the union of the | 
| 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 728 | single node entries. For example for $a$ and $\{0,1\}$ we need
 | 
| 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 729 | to take the union of $\{0,1,2\}$ (for $0$) and $\{1\}$ (for
 | 
| 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 730 | $1$). The starting state of the DFA can be calculated from the | 
| 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 731 | starting state of the NFA, that is $0$, and then do as many | 
| 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 732 | $\epsilon$-transitions as possible. This gives $\{0,1,2\}$
 | 
| 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 733 | which is the starting state of the DFA. The terminal states in | 
| 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 734 | the DFA are given by all sets that contain a $2$, which is the | 
| 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 735 | terminal state of the NFA. This completes the subset | 
| 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 736 | construction. So the corresponding DFA to the NFA from | 
| 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 737 | above is | 
| 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 738 | |
| 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 739 | \begin{center}
 | 
| 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 740 | \begin{tikzpicture}[scale=0.7,>=stealth',very thick,
 | 
| 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 741 |                     every state/.style={minimum size=0pt,
 | 
| 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 742 | draw=blue!50,very thick,fill=blue!20}, | 
| 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 743 | baseline=0mm] | 
| 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 744 | \node[state,initial,accepting]  (q012)  {$0,1,2$};
 | 
| 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 745 | \node[state,accepting] (q02) [right=of q012] {$0,2$};
 | 
| 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 746 | \node[state] (q01) [above=of q02] {$0,1$};
 | 
| 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 747 | \node[state,accepting] (q12) [below=of q02] {$1,2$};
 | 
| 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 748 | \node[state] (q0) [right=2cm of q01] {$0$};
 | 
| 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 749 | \node[state] (q1) [right=2.5cm of q02] {$1$};
 | 
| 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 750 | \node[state,accepting] (q2) [right=1.5cm of q12] {$2$};
 | 
| 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 751 | \node[state] (qn) [right=of q1] {$\{\}$};
 | 
| 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 752 | |
| 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 753 | \path[->] (q012) edge [loop below] node  {$a$} ();
 | 
| 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 754 | \path[->] (q012) edge node [above]  {$b$} (q2);
 | 
| 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 755 | \path[->] (q12) edge [bend left] node [below,pos=0.4]  {$a$} (q1);
 | 
| 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 756 | \path[->] (q12) edge node [below]  {$b$} (q2);
 | 
| 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 757 | \path[->] (q02) edge node [above]  {$a$} (q012);
 | 
| 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 758 | \path[->] (q02) edge [bend left] node [above, pos=0.8]  {$b$} (q2);
 | 
| 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 759 | \path[->] (q01) edge node [below]  {$a$} (q012);
 | 
| 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 760 | \path[->] (q01) edge [bend left] node [above]  {$b$} (q2);
 | 
| 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 761 | \path[->] (q0) edge node [below]  {$a$} (q012);
 | 
| 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 762 | \path[->] (q0) edge node [right, pos=0.2]  {$b$} (q2);
 | 
| 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 763 | \path[->] (q1) edge [loop above] node  {$a$} ();
 | 
| 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 764 | \path[->] (q1) edge node [above]  {$b$} (qn);
 | 
| 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 765 | \path[->] (q2) edge [loop right] node  {$b$} ();
 | 
| 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 766 | \path[->] (q2) edge node [below]  {$a$} (qn);
 | 
| 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 767 | \path[->] (qn) edge [loop above] node  {$a,b$} ();
 | 
| 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 768 | \end{tikzpicture}
 | 
| 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 769 | \end{center}
 | 
| 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 770 | |
| 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 771 | |
| 268 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 772 | |
| 292 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 773 | There are two points to note: One is that very often the | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 774 | resulting DFA contains a number of ``dead'' nodes that are | 
| 344 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 775 | never reachable from the starting state. For example | 
| 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 776 | there is no way to reach node $\{0,2\}$ from the starting
 | 
| 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 777 | state $\{0,1,2\}$. I let you find the other dead states.
 | 
| 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 778 | In effect the DFA in this example is not a minimal DFA. Such | 
| 292 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 779 | dead nodes can be safely removed without changing the language | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 780 | that is recognised by the DFA. Another point is that in some | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 781 | cases, however, the subset construction produces a DFA that | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 782 | does \emph{not} contain any dead nodes\ldots{}that means it
 | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 783 | calculates a minimal DFA. Which in turn means that in some | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 784 | cases the number of nodes by going from NFAs to DFAs | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 785 | exponentially increases, namely by $2^n$ (which is the number | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 786 | of subsets you can form for $n$ nodes). | 
| 268 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 787 | |
| 344 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 788 | Removing all the dead states in the automaton above, | 
| 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 789 | gives a much more legible automaton, namely | 
| 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 790 | |
| 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 791 | \begin{center}
 | 
| 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 792 | \begin{tikzpicture}[scale=0.7,>=stealth',very thick,
 | 
| 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 793 |                     every state/.style={minimum size=0pt,
 | 
| 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 794 | draw=blue!50,very thick,fill=blue!20}, | 
| 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 795 | baseline=0mm] | 
| 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 796 | \node[state,initial,accepting]  (q012)  {$0,1,2$};
 | 
| 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 797 | \node[state,accepting] (q2) [right=of q012] {$2$};
 | 
| 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 798 | \node[state] (qn) [right=of q2] {$\{\}$};
 | 
| 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 799 | |
| 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 800 | \path[->] (q012) edge [loop below] node  {$a$} ();
 | 
| 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 801 | \path[->] (q012) edge node [above]  {$b$} (q2);
 | 
| 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 802 | \path[->] (q2) edge [loop below] node  {$b$} ();
 | 
| 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 803 | \path[->] (q2) edge node [below]  {$a$} (qn);
 | 
| 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 804 | \path[->] (qn) edge [loop above] node  {$a,b$} ();
 | 
| 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 805 | \end{tikzpicture}
 | 
| 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 806 | \end{center}
 | 
| 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 807 | |
| 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 808 | \noindent Now the big question is whether this DFA | 
| 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 809 | can recognise the same language as the NFA we started with. | 
| 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 810 | I let you ponder about this question. | 
| 268 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 811 | |
| 269 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 812 | \subsubsection*{Brzozowski's Method}
 | 
| 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 813 | |
| 292 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 814 | As said before, we can also go into the other direction---from | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 815 | DFAs to regular expressions. Brzozowski's method calculates | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 816 | a regular expression using familiar transformations for | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 817 | solving equational systems. Consider the DFA: | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 818 | |
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 819 | \begin{center}
 | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 820 | \begin{tikzpicture}[scale=1.5,>=stealth',very thick,auto,
 | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 821 |                     every state/.style={minimum size=0pt,
 | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 822 | inner sep=2pt,draw=blue!50,very thick, | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 823 | fill=blue!20}] | 
| 482 | 824 |   \node[state, initial]        (q0) at ( 0,1) {$Q_0$};
 | 
| 825 |   \node[state]                    (q1) at ( 1,1) {$Q_1$};
 | |
| 826 |   \node[state, accepting] (q2) at ( 2,1) {$Q_2$};
 | |
| 292 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 827 |   \path[->] (q0) edge[bend left] node[above] {$a$} (q1)
 | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 828 |             (q1) edge[bend left] node[above] {$b$} (q0)
 | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 829 |             (q2) edge[bend left=50] node[below] {$b$} (q0)
 | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 830 |             (q1) edge node[above] {$a$} (q2)
 | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 831 |             (q2) edge [loop right] node {$a$} ()
 | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 832 |             (q0) edge [loop below] node {$b$} ();
 | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 833 | \end{tikzpicture}
 | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 834 | \end{center}
 | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 835 | |
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 836 | \noindent for which we can set up the following equational | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 837 | system | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 838 | |
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 839 | \begin{eqnarray}
 | 
| 482 | 840 | Q_0 & = & \ONE + Q_0\,b + Q_1\,b + Q_2\,b\\ | 
| 841 | Q_1 & = & Q_0\,a\\ | |
| 842 | Q_2 & = & Q_1\,a + Q_2\,a | |
| 292 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 843 | \end{eqnarray}
 | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 844 | |
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 845 | \noindent There is an equation for each node in the DFA. Let | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 846 | us have a look how the right-hand sides of the equations are | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 847 | constructed. First have a look at the second equation: the | 
| 482 | 848 | left-hand side is $Q_1$ and the right-hand side $Q_0\,a$. The | 
| 292 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 849 | right-hand side is essentially all possible ways how to end up | 
| 482 | 850 | in node $Q_1$. There is only one incoming edge from $Q_0$ consuming | 
| 322 
698ed1c96cd0
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
318diff
changeset | 851 | an $a$. Therefore the right hand side is this | 
| 482 | 852 | state followed by character---in this case $Q_0\,a$. Now lets | 
| 292 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 853 | have a look at the third equation: there are two incoming | 
| 482 | 854 | edges for $Q_2$. Therefore we have two terms, namely $Q_1\,a$ and | 
| 855 | $Q_2\,a$. These terms are separated by $+$. The first states | |
| 856 | that if in state $Q_1$ consuming an $a$ will bring you to | |
| 485 | 857 | $Q_2$, and the second that being in $Q_2$ and consuming an $a$ | 
| 482 | 858 | will make you stay in $Q_2$. The right-hand side of the | 
| 292 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 859 | first equation is constructed similarly: there are three | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 860 | incoming edges, therefore there are three terms. There is | 
| 444 
3056a4c071b0
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
349diff
changeset | 861 | one exception in that we also ``add'' $\ONE$ to the | 
| 292 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 862 | first equation, because it corresponds to the starting state | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 863 | in the DFA. | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 864 | |
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 865 | Having constructed the equational system, the question is | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 866 | how to solve it? Remarkably the rules are very similar to | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 867 | solving usual linear equational systems. For example the | 
| 482 | 868 | second equation does not contain the variable $Q_1$ on the | 
| 292 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 869 | right-hand side of the equation. We can therefore eliminate | 
| 482 | 870 | $Q_1$ from the system by just substituting this equation | 
| 292 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 871 | into the other two. This gives | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 872 | |
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 873 | \begin{eqnarray}
 | 
| 482 | 874 | Q_0 & = & \ONE + Q_0\,b + Q_0\,a\,b + Q_2\,b\\ | 
| 875 | Q_2 & = & Q_0\,a\,a + Q_2\,a | |
| 292 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 876 | \end{eqnarray}
 | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 877 | |
| 485 | 878 | \noindent where in Equation (4) we have two occurrences | 
| 482 | 879 | of $Q_0$. Like the laws about $+$ and $\cdot$, we can simplify | 
| 292 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 880 | Equation (4) to obtain the following two equations: | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 881 | |
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 882 | \begin{eqnarray}
 | 
| 482 | 883 | Q_0 & = & \ONE + Q_0\,(b + a\,b) + Q_2\,b\\ | 
| 884 | Q_2 & = & Q_0\,a\,a + Q_2\,a | |
| 292 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 885 | \end{eqnarray}
 | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 886 | |
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 887 | \noindent Unfortunately we cannot make any more progress with | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 888 | substituting equations, because both (6) and (7) contain the | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 889 | variable on the left-hand side also on the right-hand side. | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 890 | Here we need to now use a law that is different from the usual | 
| 349 
434891622131
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
344diff
changeset | 891 | laws about linear equations. It is called \emph{Arden's rule}.
 | 
| 
434891622131
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
344diff
changeset | 892 | It states that if an equation is of the form $q = q\,r + s$ | 
| 
434891622131
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
344diff
changeset | 893 | then it can be transformed to $q = s\, r^*$. Since we can | 
| 
434891622131
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
344diff
changeset | 894 | assume $+$ is symmetric, Equation (7) is of that form: $s$ is | 
| 482 | 895 | $Q_0\,a\,a$ and $r$ is $a$. That means we can transform | 
| 349 
434891622131
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
344diff
changeset | 896 | (7) to obtain the two new equations | 
| 292 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 897 | |
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 898 | \begin{eqnarray}
 | 
| 482 | 899 | Q_0 & = & \ONE + Q_0\,(b + a\,b) + Q_2\,b\\ | 
| 900 | Q_2 & = & Q_0\,a\,a\,(a^*) | |
| 292 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 901 | \end{eqnarray}
 | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 902 | |
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 903 | \noindent Now again we can substitute the second equation into | 
| 482 | 904 | the first in order to eliminate the variable $Q_2$. | 
| 292 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 905 | |
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 906 | \begin{eqnarray}
 | 
| 482 | 907 | Q_0 & = & \ONE + Q_0\,(b + a\,b) + Q_0\,a\,a\,(a^*)\,b | 
| 292 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 908 | \end{eqnarray}
 | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 909 | |
| 482 | 910 | \noindent Pulling $Q_0$ out as a single factor gives: | 
| 292 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 911 | |
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 912 | \begin{eqnarray}
 | 
| 482 | 913 | Q_0 & = & \ONE + Q_0\,(b + a\,b + a\,a\,(a^*)\,b) | 
| 292 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 914 | \end{eqnarray}
 | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 915 | |
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 916 | \noindent This equation is again of the form so that we can | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 917 | apply Arden's rule ($r$ is $b + a\,b + a\,a\,(a^*)\,b$ and $s$ | 
| 482 | 918 | is $\ONE$). This gives as solution for $Q_0$ the following | 
| 292 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 919 | regular expression: | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 920 | |
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 921 | \begin{eqnarray}
 | 
| 482 | 922 | Q_0 & = & \ONE\,(b + a\,b + a\,a\,(a^*)\,b)^* | 
| 292 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 923 | \end{eqnarray}
 | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 924 | |
| 349 
434891622131
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
344diff
changeset | 925 | \noindent Since this is a regular expression, we can simplify | 
| 444 
3056a4c071b0
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
349diff
changeset | 926 | away the $\ONE$ to obtain the slightly simpler regular | 
| 292 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 927 | expression | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 928 | |
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 929 | \begin{eqnarray}
 | 
| 482 | 930 | Q_0 & = & (b + a\,b + a\,a\,(a^*)\,b)^* | 
| 292 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 931 | \end{eqnarray}
 | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 932 | |
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 933 | \noindent | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 934 | Now we can unwind this process and obtain the solutions | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 935 | for the other equations. This gives: | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 936 | |
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 937 | \begin{eqnarray}
 | 
| 482 | 938 | Q_0 & = & (b + a\,b + a\,a\,(a^*)\,b)^*\\ | 
| 939 | Q_1 & = & (b + a\,b + a\,a\,(a^*)\,b)^*\,a\\ | |
| 940 | Q_2 & = & (b + a\,b + a\,a\,(a^*)\,b)^*\,a\,a\,(a)^* | |
| 292 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 941 | \end{eqnarray}
 | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 942 | |
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 943 | \noindent Finally, we only need to ``add'' up the equations | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 944 | which correspond to a terminal state. In our running example, | 
| 482 | 945 | this is just $Q_2$. Consequently, a regular expression | 
| 292 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 946 | that recognises the same language as the automaton is | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 947 | |
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 948 | \[ | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 949 | (b + a\,b + a\,a\,(a^*)\,b)^*\,a\,a\,(a)^* | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 950 | \] | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 951 | |
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 952 | \noindent You can somewhat crosscheck your solution | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 953 | by taking a string the regular expression can match and | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 954 | and see whether it can be matched by the automaton. | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 955 | One string for example is $aaa$ and \emph{voila} this 
 | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 956 | string is also matched by the automaton. | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 957 | |
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 958 | We should prove that Brzozowski's method really produces | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 959 | an equivalent regular expression for the automaton. But | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 960 | for the purposes of this module, we omit this. | 
| 269 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 961 | |
| 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 962 | \subsubsection*{Automata Minimization}
 | 
| 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 963 | |
| 270 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 964 | As seen in the subset construction, the translation | 
| 483 | 965 | of a NFA to a DFA can result in a rather ``inefficient'' | 
| 270 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 966 | DFA. Meaning there are states that are not needed. A | 
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 967 | DFA can be \emph{minimised} by the following algorithm:
 | 
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 968 | |
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 969 | \begin{enumerate}
 | 
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 970 | \item Take all pairs $(q, p)$ with $q \not= p$ | 
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 971 | \item Mark all pairs that accepting and non-accepting states | 
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 972 | \item For all unmarked pairs $(q, p)$ and all characters $c$ | 
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 973 | test whether | 
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 974 | |
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 975 |       \begin{center} 
 | 
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 976 | $(\delta(q, c), \delta(p,c))$ | 
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 977 |       \end{center} 
 | 
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 978 | |
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 979 | are marked. If there is one, then also mark $(q, p)$. | 
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 980 | \item Repeat last step until no change. | 
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 981 | \item All unmarked pairs can be merged. | 
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 982 | \end{enumerate}
 | 
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 983 | |
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 984 | \noindent To illustrate this algorithm, consider the following | 
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 985 | DFA. | 
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 986 | |
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 987 | \begin{center}
 | 
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 988 | \begin{tikzpicture}[>=stealth',very thick,auto,
 | 
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 989 |                     every state/.style={minimum size=0pt,
 | 
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 990 | inner sep=2pt,draw=blue!50,very thick, | 
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 991 | fill=blue!20}] | 
| 482 | 992 | \node[state,initial]  (Q_0)  {$Q_0$};
 | 
| 993 | \node[state] (Q_1) [right=of Q_0] {$Q_1$};
 | |
| 994 | \node[state] (Q_2) [below right=of Q_0] {$Q_2$};
 | |
| 995 | \node[state] (Q_3) [right=of Q_2] {$Q_3$};
 | |
| 996 | \node[state, accepting] (Q_4) [right=of Q_1] {$Q_4$};
 | |
| 997 | \path[->] (Q_0) edge node [above]  {$a$} (Q_1);
 | |
| 998 | \path[->] (Q_1) edge node [above]  {$a$} (Q_4);
 | |
| 999 | \path[->] (Q_4) edge [loop right] node  {$a, b$} ();
 | |
| 1000 | \path[->] (Q_3) edge node [right]  {$a$} (Q_4);
 | |
| 1001 | \path[->] (Q_2) edge node [above]  {$a$} (Q_3);
 | |
| 1002 | \path[->] (Q_1) edge node [right]  {$b$} (Q_2);
 | |
| 1003 | \path[->] (Q_0) edge node [above]  {$b$} (Q_2);
 | |
| 1004 | \path[->] (Q_2) edge [loop left] node  {$b$} ();
 | |
| 1005 | \path[->] (Q_3) edge [bend left=95, looseness=1.3] node | |
| 1006 |   [below]  {$b$} (Q_0);
 | |
| 270 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1007 | \end{tikzpicture}
 | 
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1008 | \end{center}
 | 
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1009 | |
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1010 | \noindent In Step 1 and 2 we consider essentially a triangle | 
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1011 | of the form | 
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1012 | |
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1013 | \begin{center}
 | 
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1014 | \begin{tikzpicture}[scale=0.6,line width=0.8mm]
 | 
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1015 | \draw (0,0) -- (4,0); | 
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1016 | \draw (0,1) -- (4,1); | 
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1017 | \draw (0,2) -- (3,2); | 
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1018 | \draw (0,3) -- (2,3); | 
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1019 | \draw (0,4) -- (1,4); | 
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1020 | |
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1021 | \draw (0,0) -- (0, 4); | 
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1022 | \draw (1,0) -- (1, 4); | 
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1023 | \draw (2,0) -- (2, 3); | 
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1024 | \draw (3,0) -- (3, 2); | 
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1025 | \draw (4,0) -- (4, 1); | 
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1026 | |
| 482 | 1027 | \draw (0.5,-0.5) node {$Q_0$}; 
 | 
| 1028 | \draw (1.5,-0.5) node {$Q_1$}; 
 | |
| 1029 | \draw (2.5,-0.5) node {$Q_2$}; 
 | |
| 1030 | \draw (3.5,-0.5) node {$Q_3$};
 | |
| 270 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1031 | |
| 482 | 1032 | \draw (-0.5, 3.5) node {$Q_1$}; 
 | 
| 1033 | \draw (-0.5, 2.5) node {$Q_2$}; 
 | |
| 1034 | \draw (-0.5, 1.5) node {$Q_3$}; 
 | |
| 1035 | \draw (-0.5, 0.5) node {$Q_4$}; 
 | |
| 270 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1036 | |
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1037 | \draw (0.5,0.5) node {\large$\star$}; 
 | 
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1038 | \draw (1.5,0.5) node {\large$\star$}; 
 | 
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1039 | \draw (2.5,0.5) node {\large$\star$}; 
 | 
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1040 | \draw (3.5,0.5) node {\large$\star$};
 | 
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1041 | \end{tikzpicture}
 | 
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1042 | \end{center}
 | 
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1043 | |
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1044 | \noindent where the lower row is filled with stars, because in | 
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1045 | the corresponding pairs there is always one state that is | 
| 482 | 1046 | accepting ($Q_4$) and a state that is non-accepting (the other | 
| 270 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1047 | states). | 
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1048 | |
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1049 | Now in Step 3 we need to fill in more stars according whether | 
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1050 | one of the next-state pairs are marked. We have to do this | 
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1051 | for every unmarked field until there is no change anymore. | 
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1052 | This gives the triangle | 
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1053 | |
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1054 | \begin{center}
 | 
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1055 | \begin{tikzpicture}[scale=0.6,line width=0.8mm]
 | 
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1056 | \draw (0,0) -- (4,0); | 
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1057 | \draw (0,1) -- (4,1); | 
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1058 | \draw (0,2) -- (3,2); | 
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1059 | \draw (0,3) -- (2,3); | 
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1060 | \draw (0,4) -- (1,4); | 
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1061 | |
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1062 | \draw (0,0) -- (0, 4); | 
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1063 | \draw (1,0) -- (1, 4); | 
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1064 | \draw (2,0) -- (2, 3); | 
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1065 | \draw (3,0) -- (3, 2); | 
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1066 | \draw (4,0) -- (4, 1); | 
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1067 | |
| 482 | 1068 | \draw (0.5,-0.5) node {$Q_0$}; 
 | 
| 1069 | \draw (1.5,-0.5) node {$Q_1$}; 
 | |
| 1070 | \draw (2.5,-0.5) node {$Q_2$}; 
 | |
| 1071 | \draw (3.5,-0.5) node {$Q_3$};
 | |
| 270 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1072 | |
| 482 | 1073 | \draw (-0.5, 3.5) node {$Q_1$}; 
 | 
| 1074 | \draw (-0.5, 2.5) node {$Q_2$}; 
 | |
| 1075 | \draw (-0.5, 1.5) node {$Q_3$}; 
 | |
| 1076 | \draw (-0.5, 0.5) node {$Q_4$}; 
 | |
| 270 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1077 | |
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1078 | \draw (0.5,0.5) node {\large$\star$}; 
 | 
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1079 | \draw (1.5,0.5) node {\large$\star$}; 
 | 
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1080 | \draw (2.5,0.5) node {\large$\star$}; 
 | 
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1081 | \draw (3.5,0.5) node {\large$\star$};
 | 
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1082 | \draw (0.5,1.5) node {\large$\star$}; 
 | 
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1083 | \draw (2.5,1.5) node {\large$\star$}; 
 | 
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1084 | \draw (0.5,3.5) node {\large$\star$}; 
 | 
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1085 | \draw (1.5,2.5) node {\large$\star$}; 
 | 
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1086 | \end{tikzpicture}
 | 
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1087 | \end{center}
 | 
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1088 | |
| 482 | 1089 | \noindent which means states $Q_0$ and $Q_2$, as well as $Q_1$ | 
| 1090 | and $Q_3$ can be merged. This gives the following minimal DFA | |
| 270 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1091 | |
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1092 | \begin{center}
 | 
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1093 | \begin{tikzpicture}[>=stealth',very thick,auto,
 | 
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1094 |                     every state/.style={minimum size=0pt,
 | 
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1095 | inner sep=2pt,draw=blue!50,very thick, | 
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1096 | fill=blue!20}] | 
| 482 | 1097 | \node[state,initial]  (Q_02)  {$Q_{0, 2}$};
 | 
| 1098 | \node[state] (Q_13) [right=of Q_02] {$Q_{1, 3}$};
 | |
| 1099 | \node[state, accepting] (Q_4) [right=of Q_13] | |
| 1100 |   {$Q_{4\phantom{,0}}$};
 | |
| 1101 | \path[->] (Q_02) edge [bend left] node [above]  {$a$} (Q_13);
 | |
| 1102 | \path[->] (Q_13) edge [bend left] node [below]  {$b$} (Q_02);
 | |
| 1103 | \path[->] (Q_02) edge [loop below] node  {$b$} ();
 | |
| 1104 | \path[->] (Q_13) edge node [above]  {$a$} (Q_4);
 | |
| 1105 | \path[->] (Q_4) edge [loop above] node  {$a, b$} ();
 | |
| 270 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1106 | \end{tikzpicture}
 | 
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1107 | \end{center}
 | 
| 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1108 | |
| 269 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 1109 | \subsubsection*{Regular Languages}
 | 
| 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 1110 | |
| 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 1111 | Given the constructions in the previous sections we obtain | 
| 349 
434891622131
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
344diff
changeset | 1112 | the following overall picture: | 
| 269 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 1113 | |
| 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 1114 | \begin{center}
 | 
| 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 1115 | \begin{tikzpicture}
 | 
| 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 1116 | \node (rexp)  {\bf Regexps};
 | 
| 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 1117 | \node (nfa) [right=of rexp] {\bf NFAs};
 | 
| 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 1118 | \node (dfa) [right=of nfa] {\bf DFAs};
 | 
| 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 1119 | \node (mdfa) [right=of dfa] {\bf\begin{tabular}{c}minimal\\ DFAs\end{tabular}};
 | 
| 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 1120 | \path[->,line width=1mm] (rexp) edge node [above=4mm, black] {\begin{tabular}{c@{\hspace{9mm}}}Thompson's\\[-1mm] construction\end{tabular}} (nfa);
 | 
| 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 1121 | \path[->,line width=1mm] (nfa) edge node [above=4mm, black] {\begin{tabular}{c}subset\\[-1mm] construction\end{tabular}}(dfa);
 | 
| 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 1122 | \path[->,line width=1mm] (dfa) edge node [below=5mm, black] {minimisation} (mdfa);
 | 
| 344 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 1123 | \path[->,line width=1mm] (dfa) edge [bend left=45] node [below] {\begin{tabular}{l}Brzozowski's\\ method\end{tabular}} (rexp);
 | 
| 269 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 1124 | \end{tikzpicture}
 | 
| 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 1125 | \end{center}
 | 
| 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 1126 | |
| 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 1127 | \noindent By going from regular expressions over NFAs to DFAs, | 
| 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 1128 | we can always ensure that for every regular expression there | 
| 349 
434891622131
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
344diff
changeset | 1129 | exists a NFA and a DFA that can recognise the same language. | 
| 269 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 1130 | Although we did not prove this fact. Similarly by going from | 
| 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 1131 | DFAs to regular expressions, we can make sure for every DFA | 
| 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 1132 | there exists a regular expression that can recognise the same | 
| 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 1133 | language. Again we did not prove this fact. | 
| 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 1134 | |
| 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 1135 | The interesting conclusion is that automata and regular | 
| 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 1136 | expressions can recognise the same set of languages: | 
| 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 1137 | |
| 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 1138 | \begin{quote} A language is \emph{regular} iff there exists a
 | 
| 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 1139 | regular expression that recognises all its strings. | 
| 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 1140 | \end{quote}
 | 
| 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 1141 | |
| 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 1142 | \noindent or equivalently | 
| 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 1143 | |
| 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 1144 | \begin{quote} A language is \emph{regular} iff there exists an
 | 
| 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 1145 | automaton that recognises all its strings. | 
| 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 1146 | \end{quote}
 | 
| 268 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 1147 | |
| 269 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 1148 | \noindent So for deciding whether a string is recognised by a | 
| 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 1149 | regular expression, we could use our algorithm based on | 
| 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 1150 | derivatives or NFAs or DFAs. But let us quickly look at what | 
| 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 1151 | the differences mean in computational terms. Translating a | 
| 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 1152 | regular expression into a NFA gives us an automaton that has | 
| 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 1153 | $O(n)$ nodes---that means the size of the NFA grows linearly | 
| 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 1154 | with the size of the regular expression. The problem with NFAs | 
| 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 1155 | is that the problem of deciding whether a string is accepted | 
| 349 
434891622131
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
344diff
changeset | 1156 | or not is computationally not cheap. Remember with NFAs we | 
| 
434891622131
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
344diff
changeset | 1157 | have potentially many next states even for the same input and | 
| 
434891622131
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
344diff
changeset | 1158 | also have the silent $\epsilon$-transitions. If we want to | 
| 483 | 1159 | find a path from the starting state of a NFA to an accepting | 
| 349 
434891622131
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
344diff
changeset | 1160 | state, we need to consider all possibilities. In Ruby and | 
| 
434891622131
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
344diff
changeset | 1161 | Python this is done by a depth-first search, which in turn | 
| 
434891622131
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
344diff
changeset | 1162 | means that if a ``wrong'' choice is made, the algorithm has to | 
| 
434891622131
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
344diff
changeset | 1163 | backtrack and thus explore all potential candidates. This is | 
| 
434891622131
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
344diff
changeset | 1164 | exactly the reason why Ruby and Python are so slow for evil | 
| 
434891622131
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
344diff
changeset | 1165 | regular expressions. An alternative to the potentially slow | 
| 
434891622131
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
344diff
changeset | 1166 | depth-first search is to explore the search space in a | 
| 
434891622131
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
344diff
changeset | 1167 | breadth-first fashion, but this might incur a big memory | 
| 269 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 1168 | penalty. | 
| 268 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 1169 | |
| 269 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 1170 | To avoid the problems with NFAs, we can translate them | 
| 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 1171 | into DFAs. With DFAs the problem of deciding whether a | 
| 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 1172 | string is recognised or not is much simpler, because in | 
| 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 1173 | each state it is completely determined what the next | 
| 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 1174 | state will be for a given input. So no search is needed. | 
| 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 1175 | The problem with this is that the translation to DFAs | 
| 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 1176 | can explode exponentially the number of states. Therefore when | 
| 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 1177 | this route is taken, we definitely need to minimise the | 
| 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 1178 | resulting DFAs in order to have an acceptable memory | 
| 344 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 1179 | and runtime behaviour. But remember the subset construction | 
| 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 1180 | in the worst case explodes the number of states by $2^n$. | 
| 349 
434891622131
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
344diff
changeset | 1181 | Effectively also the translation to DFAs can incur a big | 
| 
434891622131
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
344diff
changeset | 1182 | runtime penalty. | 
| 269 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 1183 | |
| 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 1184 | But this does not mean that everything is bad with automata. | 
| 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 1185 | Recall the problem of finding a regular expressions for the | 
| 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 1186 | language that is \emph{not} recognised by a regular
 | 
| 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 1187 | expression. In our implementation we added explicitly such a | 
| 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 1188 | regular expressions because they are useful for recognising | 
| 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 1189 | comments. But in principle we did not need to. The argument | 
| 
83e6cb90216d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
268diff
changeset | 1190 | for this is as follows: take a regular expression, translate | 
| 349 
434891622131
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
344diff
changeset | 1191 | it into a NFA and then a DFA that both recognise the same | 
| 
434891622131
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
344diff
changeset | 1192 | language. Once you have the DFA it is very easy to construct | 
| 483 | 1193 | the automaton for the language not recognised by a DFA. If | 
| 349 
434891622131
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
344diff
changeset | 1194 | the DFA is completed (this is important!), then you just need | 
| 
434891622131
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
344diff
changeset | 1195 | to exchange the accepting and non-accepting states. You can | 
| 
434891622131
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
344diff
changeset | 1196 | then translate this DFA back into a regular expression and | 
| 
434891622131
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
344diff
changeset | 1197 | that will be the regular expression that can match all strings | 
| 
434891622131
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
344diff
changeset | 1198 | the original regular expression could \emph{not} match.
 | 
| 268 
18bef085a7ca
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
251diff
changeset | 1199 | |
| 349 
434891622131
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
344diff
changeset | 1200 | It is also interesting that not all languages are regular. The | 
| 
434891622131
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
344diff
changeset | 1201 | most well-known example of a language that is not regular | 
| 
434891622131
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
344diff
changeset | 1202 | consists of all the strings of the form | 
| 292 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 1203 | |
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 1204 | \[a^n\,b^n\] | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 1205 | |
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 1206 | \noindent meaning strings that have the same number of $a$s | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 1207 | and $b$s. You can try, but you cannot find a regular | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 1208 | expression for this language and also not an automaton. One | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 1209 | can actually prove that there is no regular expression nor | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 1210 | automaton for this language, but again that would lead us too | 
| 
7ed2a25dd115
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
270diff
changeset | 1211 | far afield for what we want to do in this module. | 
| 270 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1212 | |
| 344 
408fd5994288
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
333diff
changeset | 1213 | \section*{Further Reading}
 | 
| 270 
4dbeaf43031d
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
269diff
changeset | 1214 | |
| 471 | 1215 | Compare what a ``human expert'' would create as an automaton for the | 
| 333 
8890852e18b7
updated coursework
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
325diff
changeset | 1216 | regular expression $a (b + c)^*$ and what the Thomson | 
| 
8890852e18b7
updated coursework
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
325diff
changeset | 1217 | algorithm generates. | 
| 325 
794c599cee53
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
324diff
changeset | 1218 | |
| 
794c599cee53
updated
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: 
324diff
changeset | 1219 | %http://www.inf.ed.ac.uk/teaching/courses/ct/ | 
| 140 
1be892087df2
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 1220 | \end{document}
 | 
| 
1be892087df2
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 1221 | |
| 
1be892087df2
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 1222 | %%% Local Variables: | 
| 
1be892087df2
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 1223 | %%% mode: latex | 
| 
1be892087df2
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 1224 | %%% TeX-master: t | 
| 
1be892087df2
added
 Christian Urban <christian dot urban at kcl dot ac dot uk> parents: diff
changeset | 1225 | %%% End: | 
| 482 | 1226 | |
| 1227 |