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