author | Christian Urban <urbanc@in.tum.de> |
Mon, 03 Apr 2017 01:10:54 +0800 | |
changeset 482 | 0f6e3c5a1751 |
parent 480 | 9e42ccbbd1e6 |
child 483 | 6f508bcdaa30 |
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 |
|
1be892087df2
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
6 |
|
480 | 7 |
%We can even allow ``silent |
8 |
%transitions'', also called epsilon-transitions. They allow us |
|
9 |
%to go from one state to the next without having a character |
|
10 |
%consumed. We label such silent transition with the letter |
|
11 |
%$\epsilon$. |
|
12 |
||
140
1be892087df2
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
13 |
\begin{document} |
480 | 14 |
\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
|
15 |
|
482 | 16 |
\section*{Handout 3 (Finite Automata)} |
140
1be892087df2
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
17 |
|
480 | 18 |
Every formal language and compiler course I know of bombards you first |
19 |
with automata and then to a much, much smaller extend with regular |
|
20 |
expressions. As you can see, this course is turned upside down: |
|
21 |
regular expressions come first. The reason is that regular expressions |
|
22 |
are easier to reason about and the notion of derivatives, although |
|
23 |
already quite old, only became more widely known rather |
|
24 |
recently. Still let us in this lecture have a closer look at automata |
|
25 |
and their relation to regular expressions. This will help us with |
|
26 |
understanding why the regular expression matchers in Python, Ruby and |
|
482 | 27 |
Java are so slow with certain regular expressions. |
28 |
||
29 |
||
30 |
\subsection*{Deterministic Finite Automata} |
|
31 |
||
32 |
Their definition is as follows:\medskip |
|
142
1aa28135a2da
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
141
diff
changeset
|
33 |
|
268
18bef085a7ca
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
251
diff
changeset
|
34 |
\noindent |
251
5b5a68df6d16
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
217
diff
changeset
|
35 |
A \emph{deterministic finite automaton} (DFA), say $A$, is |
482 | 36 |
given by a four-tuple written $A(Qs, Q_0, F, \delta)$ where |
142
1aa28135a2da
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
141
diff
changeset
|
37 |
|
1aa28135a2da
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
141
diff
changeset
|
38 |
\begin{itemize} |
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 |
|
50 |
exception''. That means actually we have partial functions as |
|
51 |
transitions---see our implementation later on. A typical example of a |
|
52 |
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} |
268
18bef085a7ca
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
251
diff
changeset
|
104 |
\hat{\delta}(q, []) & \dn & q\\ |
143
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
105 |
\hat{\delta}(q, c\!::\!s) & \dn & \hat{\delta}(\delta(q, c), s)\\ |
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 |
111 |
the first character of the string, follow to the next sate, then take |
|
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 |
118 |
$s$ is in the \emph{language accepted by the automaton} $A(Q, Q_0, F, |
|
119 |
\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 |
\[ |
482 | 122 |
\hat{\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 |
480 | 126 |
the set of strings accepted by an automaton. |
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 |
|
136 |
lists of \texttt{C}haracters. Since \texttt{delta} is given |
|
137 |
as partial function, it can obviously go ``wrong'' in which |
|
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 |
||
482 | 144 |
A simple Scala implementation for DFAs is given in |
145 |
Figure~\ref{dfa}. As you can see, there many features of the |
|
146 |
mathematical definition that are quite closely reflected in the |
|
147 |
code. In the DFA-class, there is a starting state, called |
|
148 |
\texttt{start}, with the polymorphic type \texttt{A}. (The reason for |
|
149 |
having a polymorphic types for the states and the input of DFAs will |
|
150 |
become clearer later.) There is a partial function \texttt{delta} for |
|
151 |
specifying the transitions. I used partial functions for representing |
|
152 |
the transitions in Scala; alternatives would have been \texttt{Maps} |
|
153 |
or even \texttt{Lists}. One of the main advantages of using partial |
|
154 |
functions is that transitions can be quite nicely defined by a series |
|
155 |
of \texttt{case} statements (see Lines 28 -- 38). If you need to |
|
156 |
represent an automaton with a sink state (catch-all-state), you can |
|
157 |
write something like |
|
143
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
158 |
|
480 | 159 |
{\small\begin{lstlisting}[language=Scala,linebackgroundcolor= |
160 |
{\ifodd\value{lstnumber}\color{capri!3}\fi}] |
|
161 |
abstract class State |
|
162 |
... |
|
163 |
case object Sink extends State |
|
164 |
||
165 |
val delta : (State, Char) :=> State = |
|
166 |
{ case (S0, 'a') => S1 |
|
167 |
case (S1, 'a') => S2 |
|
168 |
case _ => Sink |
|
169 |
} |
|
170 |
\end{lstlisting}} |
|
171 |
||
482 | 172 |
\noindent The DFA-class has also an argument for specifying final |
173 |
states. In the implementation it is a function from states to booleans |
|
174 |
(returns true whenever a state is meant to be final; false |
|
175 |
otherwise). While this boolean function is different from the sets of |
|
176 |
states used in the mathematical definition, Scala allows me to use |
|
177 |
sets for such functions (see Line 40 where I initialise the DFA). |
|
480 | 178 |
|
179 |
I let you ponder whether this is a good implementation of DFAs. In |
|
482 | 180 |
doing so I hope you notice that the $Qs$ component (the set of finite |
480 | 181 |
states) is missing from the class definition. This means that the |
182 |
implementation allows you to do some fishy things you are not meant to |
|
183 |
do with DFAs. Which fishy things could that be? |
|
184 |
||
482 | 185 |
|
186 |
||
187 |
\subsection*{Non-Deterministic Finite Automata} |
|
188 |
||
189 |
While with DFAs it will always be clear that given a state and a |
|
190 |
character what the next state is (potentially none), it will be useful |
|
191 |
to relax this restriction. That means we allow to have several |
|
192 |
potential successor states. We even allow more than one starting |
|
193 |
state. The resulting construction is called a \emph{non-deterministic |
|
194 |
finite automaton} (NFA) given also as a four-tuple $A(Qs, Q_{0s}, F, |
|
195 |
\rho)$ where |
|
143
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
196 |
|
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
197 |
\begin{itemize} |
482 | 198 |
\item $Qs$ is a finite set of states |
199 |
\item $Q_{0s}$ is a set of start states ($Q_{0s} \subseteq Qs$) |
|
200 |
\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
|
201 |
\item $\rho$ is a transition relation. |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
202 |
\end{itemize} |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
203 |
|
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
204 |
\noindent |
482 | 205 |
A typical example of an NFA is |
206 |
||
207 |
% A NFA for (ab* + b)*a |
|
143
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
208 |
\begin{center} |
482 | 209 |
\begin{tikzpicture}[scale=0.8,>=stealth',very thick, |
210 |
every state/.style={minimum size=0pt,draw=blue!50,very thick,fill=blue!20},] |
|
211 |
\node[state,initial] (Q_0) {$Q_0$}; |
|
212 |
\node[state] (Q_1) [right=of Q_0] {$Q_1$}; |
|
213 |
\node[state, accepting] (Q_2) [right=of Q_1] {$Q_2$}; |
|
214 |
\path[->] (Q_0) edge [loop above] node {$b$} (); |
|
215 |
\path[<-] (Q_0) edge node [below] {$b$} (Q_1); |
|
216 |
\path[->] (Q_0) edge [bend left] node [above] {$a$} (Q_1); |
|
217 |
\path[->] (Q_0) edge [bend right] node [below] {$a$} (Q_2); |
|
218 |
\path[->] (Q_1) edge [loop above] node {$a,b$} (); |
|
219 |
\path[->] (Q_1) edge node [above] {$a$} (Q_2); |
|
220 |
\end{tikzpicture} |
|
143
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
221 |
\end{center} |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
222 |
|
482 | 223 |
\noindent |
224 |
This NFA happens to have only one starting state, but in general there |
|
225 |
could be more. Notice that in state $Q_0$ we might go to state $Q_1$ |
|
226 |
\emph{or} to state $Q_2$ when receiving an $a$. Similarly in state $Q_1$ |
|
227 |
and receiving an $a$, we can stay in state $Q_1$ or go to $Q_2$. This |
|
228 |
kind of choice is not allowed with DFAs. When it comes to deciding |
|
229 |
whether a string is accepted by an NFA we potentially need to explore |
|
230 |
all possibilities. I let you think which kind of strings this NFA |
|
231 |
accepts. |
|
232 |
||
233 |
||
234 |
There are however a number of additional points you should note. Every |
|
235 |
DFA is a NFA, but not vice versa. The $\rho$ in NFAs is a transition |
|
236 |
\emph{relation} (DFAs have a transition function). The difference |
|
237 |
between a function and a relation is that a function has always a |
|
238 |
single output, while a relation gives, roughly speaking, several |
|
239 |
outputs. Look again at the NFA above: if you are currently in the |
|
240 |
state $Q_1$ and you read a character $b$, then you can transition to |
|
241 |
either $Q_0$ \emph{or} $Q_2$. Which route, or output, you take is not |
|
242 |
determined. This non-determinism can be represented by a relation. |
|
243 |
||
244 |
My implementation of NFAs is shown in Figure~\ref{nfa}. Perhaps |
|
245 |
interestingly, I do not use relations for my implementation of NFAs in |
|
246 |
Scala, and I also do not use transition functions which return a set |
|
247 |
of states (another popular choice for implementing NFAs). For reasons |
|
248 |
that become clear in a moment, I use sets of partial functions---see |
|
249 |
Line 7 in Figure~\ref{nfa}. \texttt{Starts} is now a set of states; |
|
250 |
\texttt{fins} is again a function from states to booleans. The |
|
251 |
\texttt{next} function calculates the set of next states reachable |
|
252 |
from a state \texttt{q} by a character \texttt{c}---we go through all |
|
253 |
partial functions in the \texttt{delta}-set, lift it (this transformes |
|
254 |
the partial function into the corresponding \texttt{Option}-function |
|
255 |
and then apply \texttt{q} and \texttt{c}. This gives us a set of |
|
256 |
\texttt{Some}s and \texttt{None}s, where the \texttt{Some}s are |
|
257 |
filtered out by the \texttt{flatMap}. Teh function \texttt{nexts} just |
|
258 |
lifts this to sets of states. \texttt{Deltas} and \texttt{accept} are |
|
259 |
similar to the DFA definitions. |
|
260 |
||
261 |
\begin{figure}[t] |
|
262 |
\small |
|
263 |
\lstinputlisting[numbers=left,linebackgroundcolor= |
|
264 |
{\ifodd\value{lstnumber}\color{capri!3}\fi}] |
|
265 |
{../progs/nfa.scala} |
|
266 |
\caption{A Scala implementation of NFAs using sets of partial functions. |
|
267 |
Notice some subtleties: \texttt{deltas} implements the delta-hat |
|
268 |
construction by lifting the transition (partial) function to |
|
269 |
lists of \texttt{C}haracters. Since \texttt{delta} is given |
|
270 |
as partial function, it can obviously go ``wrong'' in which |
|
271 |
case the \texttt{Try} in \texttt{accepts} catches the error and |
|
272 |
returns \texttt{false}---that means the string is not accepted. |
|
273 |
The example \texttt{delta} implements the DFA example shown |
|
274 |
earlier in the handout.\label{nfa}} |
|
275 |
\end{figure} |
|
276 |
||
277 |
||
278 |
||
279 |
%This means if |
|
280 |
%we need to decide whether a string is accepted by a NFA, we might have |
|
281 |
%to explore all possibilities. Also there is the special silent |
|
282 |
%transition in NFAs. As mentioned already this transition means you do |
|
283 |
%not have to ``consume'' any part of the input string, but ``silently'' |
|
284 |
%change to a different state. In the left picture, for example, if you |
|
285 |
%are in the starting state, you can silently move either to $Q_1$ or |
|
286 |
%%$Q_2$. This silent transition is also often called |
|
287 |
%\emph{$\epsilon$-transition}. |
|
268
18bef085a7ca
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
251
diff
changeset
|
288 |
|
18bef085a7ca
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
251
diff
changeset
|
289 |
|
269
83e6cb90216d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
268
diff
changeset
|
290 |
\subsubsection*{Thompson Construction} |
143
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
291 |
|
251
5b5a68df6d16
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
217
diff
changeset
|
292 |
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
|
293 |
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
|
294 |
NFAs. Consider the simple regular expressions $\ZERO$, |
3056a4c071b0
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
349
diff
changeset
|
295 |
$\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
|
296 |
|
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
297 |
\begin{center} |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
298 |
\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
|
299 |
\raisebox{1mm}{$\ZERO$} & |
143
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
300 |
\begin{tikzpicture}[scale=0.7,>=stealth',very thick, every state/.style={minimum size=3pt,draw=blue!50,very thick,fill=blue!20},] |
482 | 301 |
\node[state, initial] (Q_0) {$\mbox{}$}; |
143
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
302 |
\end{tikzpicture}\\\\ |
444
3056a4c071b0
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
349
diff
changeset
|
303 |
\raisebox{1mm}{$\ONE$} & |
143
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
304 |
\begin{tikzpicture}[scale=0.7,>=stealth',very thick, every state/.style={minimum size=3pt,draw=blue!50,very thick,fill=blue!20},] |
482 | 305 |
\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
|
306 |
\end{tikzpicture}\\\\ |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
307 |
\raisebox{2mm}{$c$} & |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
308 |
\begin{tikzpicture}[scale=0.7,>=stealth',very thick, every state/.style={minimum size=3pt,draw=blue!50,very thick,fill=blue!20},] |
482 | 309 |
\node[state, initial] (Q_0) {$\mbox{}$}; |
310 |
\node[state, accepting] (Q_1) [right=of Q_0] {$\mbox{}$}; |
|
311 |
\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
|
312 |
\end{tikzpicture}\\\\ |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
313 |
\end{tabular} |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
314 |
\end{center} |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
315 |
|
251
5b5a68df6d16
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
217
diff
changeset
|
316 |
\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
|
317 |
\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
|
318 |
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
|
319 |
|
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
320 |
\begin{center} |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
321 |
\begin{tikzpicture}[node distance=3mm, |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
322 |
>=stealth',very thick, every state/.style={minimum size=3pt,draw=blue!50,very thick,fill=blue!20},] |
482 | 323 |
\node[state, initial] (Q_0) {$\mbox{}$}; |
324 |
\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
|
325 |
\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
|
326 |
\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
|
327 |
\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
|
328 |
\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
|
329 |
\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
|
330 |
\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
|
331 |
\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
|
332 |
\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
|
333 |
\begin{pgfonlayer}{background} |
482 | 334 |
\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
|
335 |
\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
|
336 |
\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
|
337 |
\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
|
338 |
\end{pgfonlayer} |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
339 |
\end{tikzpicture} |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
340 |
\end{center} |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
341 |
|
251
5b5a68df6d16
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
217
diff
changeset
|
342 |
\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
|
343 |
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
|
344 |
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
|
345 |
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
|
346 |
non-accepting like so: |
143
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
347 |
|
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
348 |
\begin{center} |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
349 |
\begin{tikzpicture}[node distance=3mm, |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
350 |
>=stealth',very thick, every state/.style={minimum size=3pt,draw=blue!50,very thick,fill=blue!20},] |
482 | 351 |
\node[state, initial] (Q_0) {$\mbox{}$}; |
352 |
\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
|
353 |
\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
|
354 |
\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
|
355 |
\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
|
356 |
\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
|
357 |
\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
|
358 |
\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
|
359 |
\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
|
360 |
\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
|
361 |
\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
|
362 |
\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
|
363 |
\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
|
364 |
|
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
365 |
\begin{pgfonlayer}{background} |
482 | 366 |
\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
|
367 |
\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
|
368 |
\end{pgfonlayer} |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
369 |
\end{tikzpicture} |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
370 |
\end{center} |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
371 |
|
251
5b5a68df6d16
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
217
diff
changeset
|
372 |
\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
|
373 |
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
|
374 |
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
|
375 |
|
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
376 |
\begin{center} |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
377 |
\begin{tikzpicture}[node distance=3mm, |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
378 |
>=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
|
379 |
\node at (0,0) (1) {$\mbox{}$}; |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
380 |
\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
|
381 |
\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
|
382 |
|
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
383 |
\node (a) [right=of 2] {$\ldots$}; |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
384 |
\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
|
385 |
\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
|
386 |
\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
|
387 |
|
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
388 |
\node (b) [right=of 3] {$\ldots$}; |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
389 |
\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
|
390 |
\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
|
391 |
\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
|
392 |
\begin{pgfonlayer}{background} |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
393 |
\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
|
394 |
\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
|
395 |
\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
|
396 |
\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
|
397 |
\end{pgfonlayer} |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
398 |
\end{tikzpicture} |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
399 |
\end{center} |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
400 |
|
251
5b5a68df6d16
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
217
diff
changeset
|
401 |
\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
|
402 |
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
|
403 |
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
|
404 |
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
|
405 |
two starting states above, like so |
143
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
406 |
|
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
407 |
\begin{center} |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
408 |
\hspace{2cm}\begin{tikzpicture}[node distance=3mm, |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
409 |
>=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
|
410 |
\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
|
411 |
\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
|
412 |
\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
|
413 |
|
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
414 |
\node (a) [right=of 2] {$\ldots$}; |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
415 |
\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
|
416 |
\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
|
417 |
\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
|
418 |
|
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
419 |
\node (b) [right=of 3] {$\ldots$}; |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
420 |
\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
|
421 |
\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
|
422 |
\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
|
423 |
|
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
424 |
\path[->] (1) edge node [above] {$\epsilon$} (2); |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
425 |
\path[->] (1) edge node [below] {$\epsilon$} (3); |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
426 |
|
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
427 |
\begin{pgfonlayer}{background} |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
428 |
\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
|
429 |
\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
|
430 |
\end{pgfonlayer} |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
431 |
\end{tikzpicture} |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
432 |
\end{center} |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
433 |
|
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
434 |
\noindent |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
435 |
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
|
436 |
|
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
437 |
\begin{center} |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
438 |
\begin{tikzpicture}[node distance=3mm, |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
439 |
>=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
|
440 |
\node at (0,0) (1) {$\mbox{}$}; |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
441 |
\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
|
442 |
\node (a) [right=of 2] {$\ldots$}; |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
443 |
\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
|
444 |
\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
|
445 |
\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
|
446 |
\begin{pgfonlayer}{background} |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
447 |
\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
|
448 |
\node [yshift=3mm] at (1.north) {$r$}; |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
449 |
\end{pgfonlayer} |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
450 |
\end{tikzpicture} |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
451 |
\end{center} |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
452 |
|
251
5b5a68df6d16
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
217
diff
changeset
|
453 |
\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
|
454 |
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
|
455 |
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
|
456 |
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
|
457 |
|
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
458 |
\begin{center} |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
459 |
\begin{tikzpicture}[node distance=3mm, |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
460 |
>=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
|
461 |
\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
|
462 |
\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
|
463 |
\node (a) [right=of 2] {$\ldots$}; |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
464 |
\node[state] (a1) [right=of a] {$\mbox{}$}; |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
465 |
\node[state] (a2) [above=of a1] {$\mbox{}$}; |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
466 |
\node[state] (a3) [below=of a1] {$\mbox{}$}; |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
467 |
\path[->] (1) edge node [above] {$\epsilon$} (2); |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
468 |
\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
|
469 |
\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
|
470 |
\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
|
471 |
\begin{pgfonlayer}{background} |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
472 |
\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
|
473 |
\node [yshift=3mm] at (2.north) {$r^*$}; |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
474 |
\end{pgfonlayer} |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
475 |
\end{tikzpicture} |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
476 |
\end{center} |
e3fd4c5995ef
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
142
diff
changeset
|
477 |
|
251
5b5a68df6d16
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
217
diff
changeset
|
478 |
\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
|
479 |
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
|
480 |
|
268
18bef085a7ca
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
251
diff
changeset
|
481 |
|
269
83e6cb90216d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
268
diff
changeset
|
482 |
\subsubsection*{Subset Construction} |
268
18bef085a7ca
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
251
diff
changeset
|
483 |
|
292
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
484 |
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
|
485 |
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
|
486 |
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
|
487 |
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
|
488 |
|
18bef085a7ca
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
251
diff
changeset
|
489 |
\begin{center} |
18bef085a7ca
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
251
diff
changeset
|
490 |
\begin{tabular}{c@{\hspace{10mm}}c} |
18bef085a7ca
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
251
diff
changeset
|
491 |
\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
|
492 |
every state/.style={minimum size=0pt, |
18bef085a7ca
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
251
diff
changeset
|
493 |
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
|
494 |
baseline=0mm] |
482 | 495 |
\node[state,initial] (Q_0) {$0$}; |
496 |
\node[state] (Q_1) [above=of Q_0] {$1$}; |
|
497 |
\node[state, accepting] (Q_2) [below=of Q_0] {$2$}; |
|
498 |
\path[->] (Q_0) edge node [left] {$\epsilon$} (Q_1); |
|
499 |
\path[->] (Q_0) edge node [left] {$\epsilon$} (Q_2); |
|
500 |
\path[->] (Q_0) edge [loop right] node {$a$} (); |
|
501 |
\path[->] (Q_1) edge [loop above] node {$a$} (); |
|
502 |
\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
|
503 |
\end{tikzpicture} |
18bef085a7ca
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
251
diff
changeset
|
504 |
& |
18bef085a7ca
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
251
diff
changeset
|
505 |
\begin{tabular}{r|cl} |
18bef085a7ca
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
251
diff
changeset
|
506 |
nodes & $a$ & $b$\\ |
18bef085a7ca
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
251
diff
changeset
|
507 |
\hline |
344
408fd5994288
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
333
diff
changeset
|
508 |
$\{\}\phantom{\star}$ & $\{\}$ & $\{\}$\\ |
268
18bef085a7ca
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
251
diff
changeset
|
509 |
$\{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
|
510 |
$\{1\}\phantom{\star}$ & $\{1\}$ & $\{\}$\\ |
408fd5994288
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
333
diff
changeset
|
511 |
$\{2\}\star$ & $\{\}$ & $\{2\}$\\ |
268
18bef085a7ca
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
251
diff
changeset
|
512 |
$\{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
|
513 |
$\{0,2\}\star$ & $\{0,1,2\}$ & $\{2\}$\\ |
18bef085a7ca
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
251
diff
changeset
|
514 |
$\{1,2\}\star$ & $\{1\}$ & $\{2\}$\\ |
18bef085a7ca
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
251
diff
changeset
|
515 |
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
|
516 |
\end{tabular} |
18bef085a7ca
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
251
diff
changeset
|
517 |
\end{tabular} |
18bef085a7ca
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
251
diff
changeset
|
518 |
\end{center} |
18bef085a7ca
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
251
diff
changeset
|
519 |
|
18bef085a7ca
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
251
diff
changeset
|
520 |
\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
|
521 |
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
|
522 |
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
|
523 |
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
|
524 |
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
|
525 |
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
|
526 |
|
18bef085a7ca
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
251
diff
changeset
|
527 |
\begin{itemize} |
18bef085a7ca
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
251
diff
changeset
|
528 |
\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
|
529 |
$a$ and the node $\{0\}$ |
18bef085a7ca
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
251
diff
changeset
|
530 |
\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
|
531 |
\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
|
532 |
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
|
533 |
\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
|
534 |
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
|
535 |
$a$-transition |
18bef085a7ca
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
251
diff
changeset
|
536 |
\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
|
537 |
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
|
538 |
\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
|
539 |
when given an $a$ |
18bef085a7ca
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
251
diff
changeset
|
540 |
\end{itemize} |
18bef085a7ca
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
251
diff
changeset
|
541 |
|
344
408fd5994288
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
333
diff
changeset
|
542 |
\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
|
543 |
$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
|
544 |
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
|
545 |
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
|
546 |
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
|
547 |
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
|
548 |
$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
|
549 |
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
|
550 |
$\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
|
551 |
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
|
552 |
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
|
553 |
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
|
554 |
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
|
555 |
above is |
408fd5994288
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
333
diff
changeset
|
556 |
|
408fd5994288
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
333
diff
changeset
|
557 |
\begin{center} |
408fd5994288
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
333
diff
changeset
|
558 |
\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
|
559 |
every state/.style={minimum size=0pt, |
408fd5994288
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
333
diff
changeset
|
560 |
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
|
561 |
baseline=0mm] |
408fd5994288
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
333
diff
changeset
|
562 |
\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
|
563 |
\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
|
564 |
\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
|
565 |
\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
|
566 |
\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
|
567 |
\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
|
568 |
\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
|
569 |
\node[state] (qn) [right=of q1] {$\{\}$}; |
408fd5994288
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
333
diff
changeset
|
570 |
|
408fd5994288
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
333
diff
changeset
|
571 |
\path[->] (q012) edge [loop below] node {$a$} (); |
408fd5994288
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
333
diff
changeset
|
572 |
\path[->] (q012) edge node [above] {$b$} (q2); |
408fd5994288
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
333
diff
changeset
|
573 |
\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
|
574 |
\path[->] (q12) edge node [below] {$b$} (q2); |
408fd5994288
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
333
diff
changeset
|
575 |
\path[->] (q02) edge node [above] {$a$} (q012); |
408fd5994288
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
333
diff
changeset
|
576 |
\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
|
577 |
\path[->] (q01) edge node [below] {$a$} (q012); |
408fd5994288
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
333
diff
changeset
|
578 |
\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
|
579 |
\path[->] (q0) edge node [below] {$a$} (q012); |
408fd5994288
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
333
diff
changeset
|
580 |
\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
|
581 |
\path[->] (q1) edge [loop above] node {$a$} (); |
408fd5994288
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
333
diff
changeset
|
582 |
\path[->] (q1) edge node [above] {$b$} (qn); |
408fd5994288
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
333
diff
changeset
|
583 |
\path[->] (q2) edge [loop right] node {$b$} (); |
408fd5994288
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
333
diff
changeset
|
584 |
\path[->] (q2) edge node [below] {$a$} (qn); |
408fd5994288
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
333
diff
changeset
|
585 |
\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
|
586 |
\end{tikzpicture} |
408fd5994288
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
333
diff
changeset
|
587 |
\end{center} |
408fd5994288
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
333
diff
changeset
|
588 |
|
408fd5994288
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
333
diff
changeset
|
589 |
|
268
18bef085a7ca
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
251
diff
changeset
|
590 |
|
292
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
591 |
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
|
592 |
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
|
593 |
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
|
594 |
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
|
595 |
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
|
596 |
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
|
597 |
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
|
598 |
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
|
599 |
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
|
600 |
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
|
601 |
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
|
602 |
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
|
603 |
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
|
604 |
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
|
605 |
|
344
408fd5994288
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
333
diff
changeset
|
606 |
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
|
607 |
gives a much more legible automaton, namely |
408fd5994288
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
333
diff
changeset
|
608 |
|
408fd5994288
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
333
diff
changeset
|
609 |
\begin{center} |
408fd5994288
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
333
diff
changeset
|
610 |
\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
|
611 |
every state/.style={minimum size=0pt, |
408fd5994288
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
333
diff
changeset
|
612 |
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
|
613 |
baseline=0mm] |
408fd5994288
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
333
diff
changeset
|
614 |
\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
|
615 |
\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
|
616 |
\node[state] (qn) [right=of q2] {$\{\}$}; |
408fd5994288
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
333
diff
changeset
|
617 |
|
408fd5994288
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
333
diff
changeset
|
618 |
\path[->] (q012) edge [loop below] node {$a$} (); |
408fd5994288
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
333
diff
changeset
|
619 |
\path[->] (q012) edge node [above] {$b$} (q2); |
408fd5994288
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
333
diff
changeset
|
620 |
\path[->] (q2) edge [loop below] node {$b$} (); |
408fd5994288
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
333
diff
changeset
|
621 |
\path[->] (q2) edge node [below] {$a$} (qn); |
408fd5994288
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
333
diff
changeset
|
622 |
\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
|
623 |
\end{tikzpicture} |
408fd5994288
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
333
diff
changeset
|
624 |
\end{center} |
408fd5994288
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
333
diff
changeset
|
625 |
|
408fd5994288
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
333
diff
changeset
|
626 |
\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
|
627 |
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
|
628 |
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
|
629 |
|
269
83e6cb90216d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
268
diff
changeset
|
630 |
\subsubsection*{Brzozowski's Method} |
83e6cb90216d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
268
diff
changeset
|
631 |
|
292
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
632 |
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
|
633 |
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
|
634 |
a regular expression using familiar transformations for |
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
635 |
solving equational systems. Consider the DFA: |
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
636 |
|
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
637 |
\begin{center} |
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
638 |
\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
|
639 |
every state/.style={minimum size=0pt, |
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
640 |
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
|
641 |
fill=blue!20}] |
482 | 642 |
\node[state, initial] (q0) at ( 0,1) {$Q_0$}; |
643 |
\node[state] (q1) at ( 1,1) {$Q_1$}; |
|
644 |
\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
|
645 |
\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
|
646 |
(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
|
647 |
(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
|
648 |
(q1) edge node[above] {$a$} (q2) |
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
649 |
(q2) edge [loop right] node {$a$} () |
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
650 |
(q0) edge [loop below] node {$b$} (); |
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
651 |
\end{tikzpicture} |
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
652 |
\end{center} |
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
653 |
|
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
654 |
\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
|
655 |
system |
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
656 |
|
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
657 |
\begin{eqnarray} |
482 | 658 |
Q_0 & = & \ONE + Q_0\,b + Q_1\,b + Q_2\,b\\ |
659 |
Q_1 & = & Q_0\,a\\ |
|
660 |
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
|
661 |
\end{eqnarray} |
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
662 |
|
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
663 |
\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
|
664 |
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
|
665 |
constructed. First have a look at the second equation: the |
482 | 666 |
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
|
667 |
right-hand side is essentially all possible ways how to end up |
482 | 668 |
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
|
669 |
an $a$. Therefore the right hand side is this |
482 | 670 |
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
|
671 |
have a look at the third equation: there are two incoming |
482 | 672 |
edges for $Q_2$. Therefore we have two terms, namely $Q_1\,a$ and |
673 |
$Q_2\,a$. These terms are separated by $+$. The first states |
|
674 |
that if in state $Q_1$ consuming an $a$ will bring you to |
|
675 |
$Q_2$, and the secont that being in $Q_2$ and consuming an $a$ |
|
676 |
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
|
677 |
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
|
678 |
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
|
679 |
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
|
680 |
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
|
681 |
in the DFA. |
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
682 |
|
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
683 |
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
|
684 |
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
|
685 |
solving usual linear equational systems. For example the |
482 | 686 |
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
|
687 |
right-hand side of the equation. We can therefore eliminate |
482 | 688 |
$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
|
689 |
into the other two. This gives |
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
690 |
|
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
691 |
\begin{eqnarray} |
482 | 692 |
Q_0 & = & \ONE + Q_0\,b + Q_0\,a\,b + Q_2\,b\\ |
693 |
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
|
694 |
\end{eqnarray} |
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
695 |
|
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
696 |
\noindent where in Equation (4) we have two occurences |
482 | 697 |
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
|
698 |
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
|
699 |
|
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
700 |
\begin{eqnarray} |
482 | 701 |
Q_0 & = & \ONE + Q_0\,(b + a\,b) + Q_2\,b\\ |
702 |
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
|
703 |
\end{eqnarray} |
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
704 |
|
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
705 |
\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
|
706 |
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
|
707 |
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
|
708 |
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
|
709 |
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
|
710 |
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
|
711 |
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
|
712 |
assume $+$ is symmetric, Equation (7) is of that form: $s$ is |
482 | 713 |
$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
|
714 |
(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
|
715 |
|
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
716 |
\begin{eqnarray} |
482 | 717 |
Q_0 & = & \ONE + Q_0\,(b + a\,b) + Q_2\,b\\ |
718 |
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
|
719 |
\end{eqnarray} |
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
720 |
|
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
721 |
\noindent Now again we can substitute the second equation into |
482 | 722 |
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
|
723 |
|
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
724 |
\begin{eqnarray} |
482 | 725 |
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
|
726 |
\end{eqnarray} |
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
727 |
|
482 | 728 |
\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
|
729 |
|
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
730 |
\begin{eqnarray} |
482 | 731 |
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
|
732 |
\end{eqnarray} |
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
733 |
|
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
734 |
\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
|
735 |
apply Arden's rule ($r$ is $b + a\,b + a\,a\,(a^*)\,b$ and $s$ |
482 | 736 |
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
|
737 |
regular expression: |
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
738 |
|
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
739 |
\begin{eqnarray} |
482 | 740 |
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
|
741 |
\end{eqnarray} |
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
742 |
|
349
434891622131
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
344
diff
changeset
|
743 |
\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
|
744 |
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
|
745 |
expression |
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
746 |
|
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
747 |
\begin{eqnarray} |
482 | 748 |
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
|
749 |
\end{eqnarray} |
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
750 |
|
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
751 |
\noindent |
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
752 |
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
|
753 |
for the other equations. This gives: |
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
754 |
|
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
755 |
\begin{eqnarray} |
482 | 756 |
Q_0 & = & (b + a\,b + a\,a\,(a^*)\,b)^*\\ |
757 |
Q_1 & = & (b + a\,b + a\,a\,(a^*)\,b)^*\,a\\ |
|
758 |
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
|
759 |
\end{eqnarray} |
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
760 |
|
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
761 |
\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
|
762 |
which correspond to a terminal state. In our running example, |
482 | 763 |
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
|
764 |
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
|
765 |
|
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
766 |
\[ |
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
767 |
(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
|
768 |
\] |
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
769 |
|
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
770 |
\noindent You can somewhat crosscheck your solution |
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
771 |
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
|
772 |
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
|
773 |
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
|
774 |
string is also matched by the automaton. |
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 |
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
|
777 |
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
|
778 |
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
|
779 |
|
83e6cb90216d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
268
diff
changeset
|
780 |
\subsubsection*{Automata Minimization} |
83e6cb90216d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
268
diff
changeset
|
781 |
|
270
4dbeaf43031d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
269
diff
changeset
|
782 |
As seen in the subset construction, the translation |
4dbeaf43031d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
269
diff
changeset
|
783 |
of an NFA to a DFA can result in a rather ``inefficient'' |
4dbeaf43031d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
269
diff
changeset
|
784 |
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
|
785 |
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
|
786 |
|
4dbeaf43031d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
269
diff
changeset
|
787 |
\begin{enumerate} |
4dbeaf43031d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
269
diff
changeset
|
788 |
\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
|
789 |
\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
|
790 |
\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
|
791 |
test whether |
4dbeaf43031d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
269
diff
changeset
|
792 |
|
4dbeaf43031d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
269
diff
changeset
|
793 |
\begin{center} |
4dbeaf43031d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
269
diff
changeset
|
794 |
$(\delta(q, c), \delta(p,c))$ |
4dbeaf43031d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
269
diff
changeset
|
795 |
\end{center} |
4dbeaf43031d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
269
diff
changeset
|
796 |
|
4dbeaf43031d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
269
diff
changeset
|
797 |
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
|
798 |
\item Repeat last step until no change. |
4dbeaf43031d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
269
diff
changeset
|
799 |
\item All unmarked pairs can be merged. |
4dbeaf43031d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
269
diff
changeset
|
800 |
\end{enumerate} |
4dbeaf43031d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
269
diff
changeset
|
801 |
|
4dbeaf43031d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
269
diff
changeset
|
802 |
\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
|
803 |
DFA. |
4dbeaf43031d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
269
diff
changeset
|
804 |
|
4dbeaf43031d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
269
diff
changeset
|
805 |
\begin{center} |
4dbeaf43031d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
269
diff
changeset
|
806 |
\begin{tikzpicture}[>=stealth',very thick,auto, |
4dbeaf43031d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
269
diff
changeset
|
807 |
every state/.style={minimum size=0pt, |
4dbeaf43031d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
269
diff
changeset
|
808 |
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
|
809 |
fill=blue!20}] |
482 | 810 |
\node[state,initial] (Q_0) {$Q_0$}; |
811 |
\node[state] (Q_1) [right=of Q_0] {$Q_1$}; |
|
812 |
\node[state] (Q_2) [below right=of Q_0] {$Q_2$}; |
|
813 |
\node[state] (Q_3) [right=of Q_2] {$Q_3$}; |
|
814 |
\node[state, accepting] (Q_4) [right=of Q_1] {$Q_4$}; |
|
815 |
\path[->] (Q_0) edge node [above] {$a$} (Q_1); |
|
816 |
\path[->] (Q_1) edge node [above] {$a$} (Q_4); |
|
817 |
\path[->] (Q_4) edge [loop right] node {$a, b$} (); |
|
818 |
\path[->] (Q_3) edge node [right] {$a$} (Q_4); |
|
819 |
\path[->] (Q_2) edge node [above] {$a$} (Q_3); |
|
820 |
\path[->] (Q_1) edge node [right] {$b$} (Q_2); |
|
821 |
\path[->] (Q_0) edge node [above] {$b$} (Q_2); |
|
822 |
\path[->] (Q_2) edge [loop left] node {$b$} (); |
|
823 |
\path[->] (Q_3) edge [bend left=95, looseness=1.3] node |
|
824 |
[below] {$b$} (Q_0); |
|
270
4dbeaf43031d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
269
diff
changeset
|
825 |
\end{tikzpicture} |
4dbeaf43031d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
269
diff
changeset
|
826 |
\end{center} |
4dbeaf43031d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
269
diff
changeset
|
827 |
|
4dbeaf43031d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
269
diff
changeset
|
828 |
\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
|
829 |
of the form |
4dbeaf43031d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
269
diff
changeset
|
830 |
|
4dbeaf43031d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
269
diff
changeset
|
831 |
\begin{center} |
4dbeaf43031d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
269
diff
changeset
|
832 |
\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
|
833 |
\draw (0,0) -- (4,0); |
4dbeaf43031d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
269
diff
changeset
|
834 |
\draw (0,1) -- (4,1); |
4dbeaf43031d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
269
diff
changeset
|
835 |
\draw (0,2) -- (3,2); |
4dbeaf43031d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
269
diff
changeset
|
836 |
\draw (0,3) -- (2,3); |
4dbeaf43031d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
269
diff
changeset
|
837 |
\draw (0,4) -- (1,4); |
4dbeaf43031d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
269
diff
changeset
|
838 |
|
4dbeaf43031d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
269
diff
changeset
|
839 |
\draw (0,0) -- (0, 4); |
4dbeaf43031d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
269
diff
changeset
|
840 |
\draw (1,0) -- (1, 4); |
4dbeaf43031d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
269
diff
changeset
|
841 |
\draw (2,0) -- (2, 3); |
4dbeaf43031d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
269
diff
changeset
|
842 |
\draw (3,0) -- (3, 2); |
4dbeaf43031d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
269
diff
changeset
|
843 |
\draw (4,0) -- (4, 1); |
4dbeaf43031d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
269
diff
changeset
|
844 |
|
482 | 845 |
\draw (0.5,-0.5) node {$Q_0$}; |
846 |
\draw (1.5,-0.5) node {$Q_1$}; |
|
847 |
\draw (2.5,-0.5) node {$Q_2$}; |
|
848 |
\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
|
849 |
|
482 | 850 |
\draw (-0.5, 3.5) node {$Q_1$}; |
851 |
\draw (-0.5, 2.5) node {$Q_2$}; |
|
852 |
\draw (-0.5, 1.5) node {$Q_3$}; |
|
853 |
\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
|
854 |
|
4dbeaf43031d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
269
diff
changeset
|
855 |
\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
|
856 |
\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
|
857 |
\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
|
858 |
\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
|
859 |
\end{tikzpicture} |
4dbeaf43031d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
269
diff
changeset
|
860 |
\end{center} |
4dbeaf43031d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
269
diff
changeset
|
861 |
|
4dbeaf43031d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
269
diff
changeset
|
862 |
\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
|
863 |
the corresponding pairs there is always one state that is |
482 | 864 |
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
|
865 |
states). |
4dbeaf43031d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
269
diff
changeset
|
866 |
|
4dbeaf43031d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
269
diff
changeset
|
867 |
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
|
868 |
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
|
869 |
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
|
870 |
This gives the triangle |
4dbeaf43031d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
269
diff
changeset
|
871 |
|
4dbeaf43031d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
269
diff
changeset
|
872 |
\begin{center} |
4dbeaf43031d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
269
diff
changeset
|
873 |
\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
|
874 |
\draw (0,0) -- (4,0); |
4dbeaf43031d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
269
diff
changeset
|
875 |
\draw (0,1) -- (4,1); |
4dbeaf43031d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
269
diff
changeset
|
876 |
\draw (0,2) -- (3,2); |
4dbeaf43031d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
269
diff
changeset
|
877 |
\draw (0,3) -- (2,3); |
4dbeaf43031d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
269
diff
changeset
|
878 |
\draw (0,4) -- (1,4); |
4dbeaf43031d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
269
diff
changeset
|
879 |
|
4dbeaf43031d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
269
diff
changeset
|
880 |
\draw (0,0) -- (0, 4); |
4dbeaf43031d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
269
diff
changeset
|
881 |
\draw (1,0) -- (1, 4); |
4dbeaf43031d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
269
diff
changeset
|
882 |
\draw (2,0) -- (2, 3); |
4dbeaf43031d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
269
diff
changeset
|
883 |
\draw (3,0) -- (3, 2); |
4dbeaf43031d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
269
diff
changeset
|
884 |
\draw (4,0) -- (4, 1); |
4dbeaf43031d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
269
diff
changeset
|
885 |
|
482 | 886 |
\draw (0.5,-0.5) node {$Q_0$}; |
887 |
\draw (1.5,-0.5) node {$Q_1$}; |
|
888 |
\draw (2.5,-0.5) node {$Q_2$}; |
|
889 |
\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
|
890 |
|
482 | 891 |
\draw (-0.5, 3.5) node {$Q_1$}; |
892 |
\draw (-0.5, 2.5) node {$Q_2$}; |
|
893 |
\draw (-0.5, 1.5) node {$Q_3$}; |
|
894 |
\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
|
895 |
|
4dbeaf43031d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
269
diff
changeset
|
896 |
\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
|
897 |
\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
|
898 |
\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
|
899 |
\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
|
900 |
\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
|
901 |
\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
|
902 |
\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
|
903 |
\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
|
904 |
\end{tikzpicture} |
4dbeaf43031d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
269
diff
changeset
|
905 |
\end{center} |
4dbeaf43031d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
269
diff
changeset
|
906 |
|
482 | 907 |
\noindent which means states $Q_0$ and $Q_2$, as well as $Q_1$ |
908 |
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
|
909 |
|
4dbeaf43031d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
269
diff
changeset
|
910 |
\begin{center} |
4dbeaf43031d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
269
diff
changeset
|
911 |
\begin{tikzpicture}[>=stealth',very thick,auto, |
4dbeaf43031d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
269
diff
changeset
|
912 |
every state/.style={minimum size=0pt, |
4dbeaf43031d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
269
diff
changeset
|
913 |
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
|
914 |
fill=blue!20}] |
482 | 915 |
\node[state,initial] (Q_02) {$Q_{0, 2}$}; |
916 |
\node[state] (Q_13) [right=of Q_02] {$Q_{1, 3}$}; |
|
917 |
\node[state, accepting] (Q_4) [right=of Q_13] |
|
918 |
{$Q_{4\phantom{,0}}$}; |
|
919 |
\path[->] (Q_02) edge [bend left] node [above] {$a$} (Q_13); |
|
920 |
\path[->] (Q_13) edge [bend left] node [below] {$b$} (Q_02); |
|
921 |
\path[->] (Q_02) edge [loop below] node {$b$} (); |
|
922 |
\path[->] (Q_13) edge node [above] {$a$} (Q_4); |
|
923 |
\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
|
924 |
\end{tikzpicture} |
4dbeaf43031d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
269
diff
changeset
|
925 |
\end{center} |
4dbeaf43031d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
269
diff
changeset
|
926 |
|
269
83e6cb90216d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
268
diff
changeset
|
927 |
\subsubsection*{Regular Languages} |
83e6cb90216d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
268
diff
changeset
|
928 |
|
83e6cb90216d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
268
diff
changeset
|
929 |
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
|
930 |
the following overall picture: |
269
83e6cb90216d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
268
diff
changeset
|
931 |
|
83e6cb90216d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
268
diff
changeset
|
932 |
\begin{center} |
83e6cb90216d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
268
diff
changeset
|
933 |
\begin{tikzpicture} |
83e6cb90216d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
268
diff
changeset
|
934 |
\node (rexp) {\bf Regexps}; |
83e6cb90216d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
268
diff
changeset
|
935 |
\node (nfa) [right=of rexp] {\bf NFAs}; |
83e6cb90216d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
268
diff
changeset
|
936 |
\node (dfa) [right=of nfa] {\bf DFAs}; |
83e6cb90216d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
268
diff
changeset
|
937 |
\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
|
938 |
\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
|
939 |
\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
|
940 |
\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
|
941 |
\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
|
942 |
\end{tikzpicture} |
83e6cb90216d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
268
diff
changeset
|
943 |
\end{center} |
83e6cb90216d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
268
diff
changeset
|
944 |
|
83e6cb90216d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
268
diff
changeset
|
945 |
\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
|
946 |
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
|
947 |
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
|
948 |
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
|
949 |
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
|
950 |
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
|
951 |
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
|
952 |
|
83e6cb90216d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
268
diff
changeset
|
953 |
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
|
954 |
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
|
955 |
|
83e6cb90216d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
268
diff
changeset
|
956 |
\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
|
957 |
regular expression that recognises all its strings. |
83e6cb90216d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
268
diff
changeset
|
958 |
\end{quote} |
83e6cb90216d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
268
diff
changeset
|
959 |
|
83e6cb90216d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
268
diff
changeset
|
960 |
\noindent or equivalently |
83e6cb90216d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
268
diff
changeset
|
961 |
|
83e6cb90216d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
268
diff
changeset
|
962 |
\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
|
963 |
automaton that recognises all its strings. |
83e6cb90216d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
268
diff
changeset
|
964 |
\end{quote} |
268
18bef085a7ca
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
251
diff
changeset
|
965 |
|
269
83e6cb90216d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
268
diff
changeset
|
966 |
\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
|
967 |
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
|
968 |
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
|
969 |
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
|
970 |
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
|
971 |
$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
|
972 |
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
|
973 |
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
|
974 |
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
|
975 |
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
|
976 |
also have the silent $\epsilon$-transitions. If we want to |
434891622131
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
344
diff
changeset
|
977 |
find a path from the starting state of an NFA to an accepting |
434891622131
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
344
diff
changeset
|
978 |
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
|
979 |
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
|
980 |
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
|
981 |
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
|
982 |
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
|
983 |
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
|
984 |
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
|
985 |
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
|
986 |
penalty. |
268
18bef085a7ca
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
251
diff
changeset
|
987 |
|
269
83e6cb90216d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
268
diff
changeset
|
988 |
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
|
989 |
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
|
990 |
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
|
991 |
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
|
992 |
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
|
993 |
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
|
994 |
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
|
995 |
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
|
996 |
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
|
997 |
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
|
998 |
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
|
999 |
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
|
1000 |
runtime penalty. |
269
83e6cb90216d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
268
diff
changeset
|
1001 |
|
83e6cb90216d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
268
diff
changeset
|
1002 |
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
|
1003 |
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
|
1004 |
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
|
1005 |
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
|
1006 |
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
|
1007 |
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
|
1008 |
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
|
1009 |
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
|
1010 |
language. Once you have the DFA it is very easy to construct |
434891622131
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
344
diff
changeset
|
1011 |
the automaton for the language not recognised by an DFA. If |
434891622131
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
344
diff
changeset
|
1012 |
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
|
1013 |
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
|
1014 |
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
|
1015 |
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
|
1016 |
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
|
1017 |
|
349
434891622131
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
344
diff
changeset
|
1018 |
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
|
1019 |
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
|
1020 |
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
|
1021 |
|
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1022 |
\[a^n\,b^n\] |
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1023 |
|
7ed2a25dd115
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
270
diff
changeset
|
1024 |
\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
|
1025 |
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
|
1026 |
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
|
1027 |
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
|
1028 |
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
|
1029 |
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
|
1030 |
|
344
408fd5994288
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
333
diff
changeset
|
1031 |
\section*{Further Reading} |
270
4dbeaf43031d
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
269
diff
changeset
|
1032 |
|
471 | 1033 |
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
|
1034 |
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
|
1035 |
algorithm generates. |
325
794c599cee53
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
324
diff
changeset
|
1036 |
|
794c599cee53
updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
324
diff
changeset
|
1037 |
%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
|
1038 |
\end{document} |
1be892087df2
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
1039 |
|
1be892087df2
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
1040 |
%%% Local Variables: |
1be892087df2
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
1041 |
%%% mode: latex |
1be892087df2
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
1042 |
%%% TeX-master: t |
1be892087df2
added
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff
changeset
|
1043 |
%%% End: |
482 | 1044 |
|
1045 |
||
1046 |
\noindent |
|
1047 |
Two typical examples of NFAs are |
|
1048 |
\begin{center} |
|
1049 |
\begin{tabular}[t]{c@{\hspace{9mm}}c} |
|
1050 |
\begin{tikzpicture}[scale=0.7,>=stealth',very thick, |
|
1051 |
every state/.style={minimum size=0pt,draw=blue!50,very thick,fill=blue!20},] |
|
1052 |
\node[state,initial] (Q_0) {$Q_0$}; |
|
1053 |
\node[state] (Q_1) [above=of Q_0] {$Q_1$}; |
|
1054 |
\node[state, accepting] (Q_2) [below=of Q_0] {$Q_2$}; |
|
1055 |
\path[->] (Q_0) edge node [left] {$\epsilon$} (Q_1); |
|
1056 |
\path[->] (Q_0) edge node [left] {$\epsilon$} (Q_2); |
|
1057 |
\path[->] (Q_0) edge [loop right] node {$a$} (); |
|
1058 |
\path[->] (Q_1) edge [loop above] node {$a$} (); |
|
1059 |
\path[->] (Q_2) edge [loop below] node {$b$} (); |
|
1060 |
\end{tikzpicture} & |
|
1061 |
||
1062 |
\raisebox{20mm}{ |
|
1063 |
\begin{tikzpicture}[scale=0.7,>=stealth',very thick, |
|
1064 |
every state/.style={minimum size=0pt,draw=blue!50,very thick,fill=blue!20},] |
|
1065 |
\node[state,initial] (r_1) {$r_1$}; |
|
1066 |
\node[state] (r_2) [above=of r_1] {$r_2$}; |
|
1067 |
\node[state, accepting] (r_3) [right=of r_1] {$r_3$}; |
|
1068 |
\path[->] (r_1) edge node [below] {$b$} (r_3); |
|
1069 |
\path[->] (r_2) edge [bend left] node [above] {$a$} (r_3); |
|
1070 |
\path[->] (r_1) edge [bend left] node [left] {$\epsilon$} (r_2); |
|
1071 |
\path[->] (r_2) edge [bend left] node [right] {$a$} (r_1); |
|
1072 |
\end{tikzpicture}} |
|
1073 |
\end{tabular} |
|
1074 |
\end{center} |