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