handouts/ho03.tex
author Christian Urban <urbanc@in.tum.de>
Wed, 22 Mar 2017 14:10:01 +0000
changeset 480 9e42ccbbd1e6
parent 471 9476086849ad
child 482 0f6e3c5a1751
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
140
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
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     6
480
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
     7
%We can even allow ``silent
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
     8
%transitions'', also called epsilon-transitions. They allow us
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
     9
%to go from one state to the next without having a character
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
    10
%consumed. We label such silent transition with the letter
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
    11
%$\epsilon$.
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
    12
140
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    13
\begin{document}
480
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
    14
\fnote{\copyright{} Christian Urban, King's College London, 2014, 2015, 2016, 2017}
140
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    15
268
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
    16
\section*{Handout 3 (Automata)}
140
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    17
480
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
    18
Every formal language and compiler course I know of bombards you first
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
    19
with automata and then to a much, much smaller extend with regular
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
    20
expressions. As you can see, this course is turned upside down:
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
    21
regular expressions come first. The reason is that regular expressions
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
    22
are easier to reason about and the notion of derivatives, although
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
    23
already quite old, only became more widely known rather
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
    24
recently. Still let us in this lecture have a closer look at automata
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
    25
and their relation to regular expressions. This will help us with
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
    26
understanding why the regular expression matchers in Python, Ruby and
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
    27
Java are so slow with certain regular expressions. The central
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
    28
definition is:\medskip
142
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 141
diff changeset
    29
268
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
    30
\noindent 
251
5b5a68df6d16 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 217
diff changeset
    31
A \emph{deterministic finite automaton} (DFA), say $A$, is
480
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
    32
given by a four-tuple written $A(Q, q_0, F, \delta)$ where
142
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 141
diff changeset
    33
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 141
diff changeset
    34
\begin{itemize}
268
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
    35
\item $Q$ is a finite set of states,
142
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 141
diff changeset
    36
\item $q_0 \in Q$ is the start state,
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 141
diff changeset
    37
\item $F \subseteq Q$ are the accepting states, and
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 141
diff changeset
    38
\item $\delta$ is the transition function.
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 141
diff changeset
    39
\end{itemize}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 141
diff changeset
    40
251
5b5a68df6d16 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 217
diff changeset
    41
\noindent The transition function determines how to
5b5a68df6d16 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 217
diff changeset
    42
``transition'' from one state to the next state with respect
268
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
    43
to a character. We have the assumption that these transition
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
    44
functions do not need to be defined everywhere: so it can be
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
    45
the case that given a character there is no next state, in
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
    46
which case we need to raise a kind of ``failure exception''. A
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
    47
typical example of a DFA is
142
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 141
diff changeset
    48
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 141
diff changeset
    49
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 141
diff changeset
    50
\begin{tikzpicture}[>=stealth',very thick,auto,
268
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
    51
                    every state/.style={minimum size=0pt,
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
    52
                    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
    53
                    fill=blue!20},scale=2]
143
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
    54
\node[state,initial]  (q_0)  {$q_0$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
    55
\node[state] (q_1) [right=of q_0] {$q_1$};
142
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 141
diff changeset
    56
\node[state] (q_2) [below right=of q_0] {$q_2$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 141
diff changeset
    57
\node[state] (q_3) [right=of q_2] {$q_3$};
143
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
    58
\node[state, accepting] (q_4) [right=of q_1] {$q_4$};
142
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 141
diff changeset
    59
\path[->] (q_0) edge node [above]  {$a$} (q_1);
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 141
diff changeset
    60
\path[->] (q_1) edge node [above]  {$a$} (q_4);
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 141
diff changeset
    61
\path[->] (q_4) edge [loop right] node  {$a, b$} ();
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 141
diff changeset
    62
\path[->] (q_3) edge node [right]  {$a$} (q_4);
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 141
diff changeset
    63
\path[->] (q_2) edge node [above]  {$a$} (q_3);
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 141
diff changeset
    64
\path[->] (q_1) edge node [right]  {$b$} (q_2);
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 141
diff changeset
    65
\path[->] (q_0) edge node [above]  {$b$} (q_2);
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 141
diff changeset
    66
\path[->] (q_2) edge [loop left] node  {$b$} ();
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 141
diff changeset
    67
\path[->] (q_3) edge [bend left=95, looseness=1.3] node [below]  {$b$} (q_0);
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 141
diff changeset
    68
\end{tikzpicture}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 141
diff changeset
    69
\end{center}
140
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    70
268
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
    71
\noindent In this graphical notation, the accepting state
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
    72
$q_4$ is indicated with double circles. Note that there can be
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
    73
more than one accepting state. It is also possible that a DFA
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
    74
has no accepting states at all, or that the starting state is
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
    75
also an accepting state. In the case above the transition
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
    76
function is defined everywhere and can be given as a table as
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
    77
follows:
143
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
    78
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
    79
\[
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
    80
\begin{array}{lcl}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
    81
(q_0, a) &\rightarrow& q_1\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
    82
(q_0, b) &\rightarrow& q_2\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
    83
(q_1, a) &\rightarrow& q_4\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
    84
(q_1, b) &\rightarrow& q_2\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
    85
(q_2, a) &\rightarrow& q_3\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
    86
(q_2, b) &\rightarrow& q_2\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
    87
(q_3, a) &\rightarrow& q_4\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
    88
(q_3, b) &\rightarrow& q_0\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
    89
(q_4, a) &\rightarrow& q_4\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
    90
(q_4, b) &\rightarrow& q_4\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
    91
\end{array}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
    92
\]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
    93
268
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
    94
We need to define the notion of what language is accepted by
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
    95
an automaton. For this we lift the transition function
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
    96
$\delta$ from characters to strings as follows:
143
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
    97
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
    98
\[
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
    99
\begin{array}{lcl}
268
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
   100
\hat{\delta}(q, [])        & \dn & q\\
143
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   101
\hat{\delta}(q, c\!::\!s) & \dn & \hat{\delta}(\delta(q, c), s)\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   102
\end{array}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   103
\]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   104
268
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
   105
\noindent This lifted transition function is often called
480
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   106
``delta-hat''. Given a string, we start in the starting state and take
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   107
the first character of the string, follow to the next sate, then take
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   108
the second character and so on. Once the string is exhausted and we
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   109
end up in an accepting state, then this string is accepted by the
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   110
automaton. Otherwise it is not accepted. This also means that if along
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   111
the way we hit the case where the transition function $\delta$ is not
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   112
defined, we need to raise an error. In our implementation we will deal
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   113
with this case elegantly by using Scala's \texttt{Try}. So $s$ is in
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   114
the \emph{language accepted by the automaton} $A(Q, q_0, F, \delta)$
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   115
iff
143
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   116
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   117
\[
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   118
\hat{\delta}(q_0, s) \in F 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   119
\]
268
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
   120
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
   121
\noindent I let you think about a definition that describes
480
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   122
the set of strings accepted by an automaton. 
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   123
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   124
\begin{figure}[p]
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   125
\small
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   126
\lstinputlisting[numbers=left,linebackgroundcolor=
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   127
                  {\ifodd\value{lstnumber}\color{capri!3}\fi}]
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   128
                  {../progs/dfa.scala}
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   129
\caption{Scala implementation of DFAs using partial functions.
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   130
  Notice some subtleties: \texttt{deltas} implements the delta-hat
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   131
  construction by lifting the transition (partial) function to
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   132
  lists of \texttt{C}haracters. Since \texttt{delta} is given
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   133
  as partial function, it can obviously go ``wrong'' in which
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   134
  case the \texttt{Try} in \texttt{accepts} catches the error and
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   135
  returns \texttt{false}, that is the string is not accepted.
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   136
  The example implements the DFA example shown above.\label{dfa}}
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   137
\end{figure}
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   138
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   139
A simple Scala implementation of DFAs is given in Figure~\ref{dfa}. As
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   140
you can see, there many features of the mathematical definition that
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   141
are quite closely reflected in the code. In the DFA-class, there is a
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   142
starting state, called \texttt{start}, with the polymorphic type
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   143
\texttt{A}. There is a partial function \texttt{delta} for specifying
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   144
the transitions. I used partial functions for transitions in Scala;
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   145
alternatives would have been \texttt{Maps} or even \texttt{Lists}. One
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   146
of the main advantages of using partial functions is that transitions
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   147
can be quite nicely defined by a series of \texttt{case} statements
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   148
(see Lines 28 -- 38). If you need to represent an automaton
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   149
with a sink state (catch-all-state), you can write something like
143
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   150
480
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   151
{\small\begin{lstlisting}[language=Scala,linebackgroundcolor=
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   152
                  {\ifodd\value{lstnumber}\color{capri!3}\fi}]
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   153
abstract class State
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   154
...
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   155
case object Sink extends State
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   156
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   157
val delta : (State, Char) :=> State = 
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   158
  { case (S0, 'a') => S1
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   159
    case (S1, 'a') => S2
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   160
    case _ => Sink
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   161
  }
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   162
\end{lstlisting}}  
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   163
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   164
\noindent The DFA-class has also an argument for final states. It is a
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   165
function from states to booleans (returns true whenever a state is
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   166
meant to be finbal; false otherwise). While this is a boolean
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   167
function, Scala allows me to use sets of states for such functions
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   168
(see Line 40 where I initialise the DFA). 
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   169
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   170
I let you ponder whether this is a good implementation of DFAs. In
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   171
doing so I hope you notice that the $Q$ component (the set of finite
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   172
states) is missing from the class definition. This means that the
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   173
implementation allows you to do some fishy things you are not meant to
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   174
do with DFAs. Which fishy things could that be?
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   175
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   176
While with DFAs it will always be clear that given a character what
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   177
the next state is (potentially none), it will be useful to relax this
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   178
restriction. That means we have several potential successor states. We
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   179
even allow more than one starting state. The resulting construction is
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   180
called a \emph{non-deterministic finite automaton} (NFA) given also as
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   181
a four-tuple $A(Q, Q_{0s}, F, \rho)$ where
143
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   182
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   183
\begin{itemize}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   184
\item $Q$ is a finite set of states
480
9e42ccbbd1e6 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
   185
\item $Q_{0s}$ are the start states ($Q_{0s} \subseteq Q$)
143
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   186
\item $F$ are some accepting states with $F \subseteq Q$, and
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   187
\item $\rho$ is a transition relation.
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   188
\end{itemize}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   189
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   190
\noindent
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   191
Two typical examples of NFAs are
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   192
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   193
\begin{tabular}[t]{c@{\hspace{9mm}}c}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   194
\begin{tikzpicture}[scale=0.7,>=stealth',very thick,
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   195
                             every state/.style={minimum size=0pt,draw=blue!50,very thick,fill=blue!20},]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   196
\node[state,initial]  (q_0)  {$q_0$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   197
\node[state] (q_1) [above=of q_0] {$q_1$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   198
\node[state, accepting] (q_2) [below=of q_0] {$q_2$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   199
\path[->] (q_0) edge node [left]  {$\epsilon$} (q_1);
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   200
\path[->] (q_0) edge node [left]  {$\epsilon$} (q_2);
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   201
\path[->] (q_0) edge [loop right] node  {$a$} ();
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   202
\path[->] (q_1) edge [loop above] node  {$a$} ();
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   203
\path[->] (q_2) edge [loop below] node  {$b$} ();
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   204
\end{tikzpicture} &
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   205
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   206
\raisebox{20mm}{
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   207
\begin{tikzpicture}[scale=0.7,>=stealth',very thick,
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   208
                             every state/.style={minimum size=0pt,draw=blue!50,very thick,fill=blue!20},]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   209
\node[state,initial]  (r_1)  {$r_1$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   210
\node[state] (r_2) [above=of r_1] {$r_2$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   211
\node[state, accepting] (r_3) [right=of r_1] {$r_3$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   212
\path[->] (r_1) edge node [below]  {$b$} (r_3);
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   213
\path[->] (r_2) edge [bend left] node [above]  {$a$} (r_3);
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   214
\path[->] (r_1) edge [bend left] node  [left] {$\epsilon$} (r_2);
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   215
\path[->] (r_2) edge [bend left] node  [right] {$a$} (r_1);
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   216
\end{tikzpicture}}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   217
\end{tabular}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   218
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   219
268
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
   220
\noindent There are, however, a number of points you should
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
   221
note. Every DFA is a NFA, but not vice versa. The $\rho$ in
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
   222
NFAs is a transition \emph{relation} (DFAs have a transition
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
   223
function). The difference between a function and a relation is
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
   224
that a function has always a single output, while a relation
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
   225
gives, roughly speaking, several outputs. Look at the NFA on
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
   226
the right-hand side above: if you are currently in the state
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
   227
$r_2$ and you read a character $a$, then you can transition to
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
   228
either $r_1$ \emph{or} $r_3$. Which route you take is not
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
   229
determined. This means if we need to decide whether a string
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
   230
is accepted by a NFA, we might have to explore all
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
   231
possibilities. Also there is the special silent transition in
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
   232
NFAs. As mentioned already this transition means you do not
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
   233
have to ``consume'' any part of the input string, but
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
   234
``silently'' change to a different state. In the left picture,
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
   235
for example, if you are in the starting state, you can 
459
780486571e38 updated
Christian Urban <urbanc@in.tum.de>
parents: 444
diff changeset
   236
silently move either to $q_1$ or $q_2$. This silent
780486571e38 updated
Christian Urban <urbanc@in.tum.de>
parents: 444
diff changeset
   237
transition is also often called \emph{$\epsilon$-transition}.
268
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
   238
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
   239
269
83e6cb90216d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 268
diff changeset
   240
\subsubsection*{Thompson Construction}
143
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   241
251
5b5a68df6d16 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 217
diff changeset
   242
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
   243
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
   244
NFAs. Consider the simple regular expressions $\ZERO$,
3056a4c071b0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 349
diff changeset
   245
$\ONE$ and $c$. They can be translated as follows:
143
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   246
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   247
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   248
\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
   249
\raisebox{1mm}{$\ZERO$} & 
143
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   250
\begin{tikzpicture}[scale=0.7,>=stealth',very thick, every state/.style={minimum size=3pt,draw=blue!50,very thick,fill=blue!20},]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   251
\node[state, initial]  (q_0)  {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   252
\end{tikzpicture}\\\\
444
3056a4c071b0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 349
diff changeset
   253
\raisebox{1mm}{$\ONE$} & 
143
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   254
\begin{tikzpicture}[scale=0.7,>=stealth',very thick, every state/.style={minimum size=3pt,draw=blue!50,very thick,fill=blue!20},]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   255
\node[state, initial, accepting]  (q_0)  {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   256
\end{tikzpicture}\\\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   257
\raisebox{2mm}{$c$} & 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   258
\begin{tikzpicture}[scale=0.7,>=stealth',very thick, every state/.style={minimum size=3pt,draw=blue!50,very thick,fill=blue!20},]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   259
\node[state, initial]  (q_0)  {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   260
\node[state, accepting]  (q_1)  [right=of q_0] {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   261
\path[->] (q_0) edge node [below]  {$c$} (q_1);
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   262
\end{tikzpicture}\\\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   263
\end{tabular}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   264
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   265
251
5b5a68df6d16 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 217
diff changeset
   266
\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
   267
\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
   268
automata representing $r_1$ and $r_2$ respectively. 
143
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   269
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   270
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   271
\begin{tikzpicture}[node distance=3mm,
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   272
                             >=stealth',very thick, every state/.style={minimum size=3pt,draw=blue!50,very thick,fill=blue!20},]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   273
\node[state, initial]  (q_0)  {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   274
\node (r_1)  [right=of q_0] {$\ldots$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   275
\node[state, accepting]  (t_1)  [right=of r_1] {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   276
\node[state, accepting]  (t_2)  [above=of t_1] {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   277
\node[state, accepting]  (t_3)  [below=of t_1] {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   278
\node[state, initial]  (a_0)  [right=2.5cm of t_1] {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   279
\node (b_1)  [right=of a_0] {$\ldots$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   280
\node[state, accepting]  (c_1)  [right=of b_1] {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   281
\node[state, accepting]  (c_2)  [above=of c_1] {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   282
\node[state, accepting]  (c_3)  [below=of c_1] {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   283
\begin{pgfonlayer}{background}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   284
\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)] {};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   285
\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)] {};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   286
\node [yshift=2mm] at (1.north) {$r_1$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   287
\node [yshift=2mm] at (2.north) {$r_2$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   288
\end{pgfonlayer}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   289
\end{tikzpicture}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   290
\end{center}
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
\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
   293
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
   294
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
   295
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
   296
non-accepting like so:
143
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   297
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   298
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   299
\begin{tikzpicture}[node distance=3mm,
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   300
                             >=stealth',very thick, every state/.style={minimum size=3pt,draw=blue!50,very thick,fill=blue!20},]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   301
\node[state, initial]  (q_0)  {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   302
\node (r_1)  [right=of q_0] {$\ldots$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   303
\node[state]  (t_1)  [right=of r_1] {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   304
\node[state]  (t_2)  [above=of t_1] {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   305
\node[state]  (t_3)  [below=of t_1] {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   306
\node[state]  (a_0)  [right=2.5cm of t_1] {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   307
\node (b_1)  [right=of a_0] {$\ldots$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   308
\node[state, accepting]  (c_1)  [right=of b_1] {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   309
\node[state, accepting]  (c_2)  [above=of c_1] {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   310
\node[state, accepting]  (c_3)  [below=of c_1] {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   311
\path[->] (t_1) edge node [above, pos=0.3]  {$\epsilon$} (a_0);
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   312
\path[->] (t_2) edge node [above]  {$\epsilon$} (a_0);
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   313
\path[->] (t_3) edge node [below]  {$\epsilon$} (a_0);
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   314
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   315
\begin{pgfonlayer}{background}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   316
\node (3) [rounded corners, inner sep=1mm, thick, draw=black!60, fill=black!20, fit= (q_0) (c_1) (c_2) (c_3)] {};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   317
\node [yshift=2mm] at (3.north) {$r_1\cdot r_2$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   318
\end{pgfonlayer}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   319
\end{tikzpicture}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   320
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   321
251
5b5a68df6d16 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 217
diff changeset
   322
\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
   323
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
   324
automata representing $r_1$ and $r_2$ respectively. 
143
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   325
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   326
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   327
\begin{tikzpicture}[node distance=3mm,
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   328
                             >=stealth',very thick, every state/.style={minimum size=3pt,draw=blue!50,very thick,fill=blue!20},]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   329
\node at (0,0)  (1)  {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   330
\node[state, initial]  (2)  [above right=16mm of 1] {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   331
\node[state, initial]  (3)  [below right=16mm of 1] {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   332
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   333
\node (a)  [right=of 2] {$\ldots$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   334
\node[state, accepting]  (a1)  [right=of a] {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   335
\node[state, accepting]  (a2)  [above=of a1] {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   336
\node[state, accepting]  (a3)  [below=of a1] {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   337
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   338
\node (b)  [right=of 3] {$\ldots$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   339
\node[state, accepting]  (b1)  [right=of b] {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   340
\node[state, accepting]  (b2)  [above=of b1] {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   341
\node[state, accepting]  (b3)  [below=of b1] {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   342
\begin{pgfonlayer}{background}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   343
\node (1) [rounded corners, inner sep=1mm, thick, draw=black!60, fill=black!20, fit= (2) (a1) (a2) (a3)] {};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   344
\node (2) [rounded corners, inner sep=1mm, thick, draw=black!60, fill=black!20, fit= (3) (b1) (b2) (b3)] {};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   345
\node [yshift=3mm] at (1.north) {$r_1$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   346
\node [yshift=3mm] at (2.north) {$r_2$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   347
\end{pgfonlayer}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   348
\end{tikzpicture}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   349
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   350
251
5b5a68df6d16 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 217
diff changeset
   351
\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
   352
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
   353
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
   354
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
   355
two starting states above, like so
143
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   356
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   357
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   358
\hspace{2cm}\begin{tikzpicture}[node distance=3mm,
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   359
                             >=stealth',very thick, every state/.style={minimum size=3pt,draw=blue!50,very thick,fill=blue!20},]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   360
\node at (0,0) [state, initial]  (1)  {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   361
\node[state]  (2)  [above right=16mm of 1] {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   362
\node[state]  (3)  [below right=16mm of 1] {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   363
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   364
\node (a)  [right=of 2] {$\ldots$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   365
\node[state, accepting]  (a1)  [right=of a] {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   366
\node[state, accepting]  (a2)  [above=of a1] {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   367
\node[state, accepting]  (a3)  [below=of a1] {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   368
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   369
\node (b)  [right=of 3] {$\ldots$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   370
\node[state, accepting]  (b1)  [right=of b] {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   371
\node[state, accepting]  (b2)  [above=of b1] {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   372
\node[state, accepting]  (b3)  [below=of b1] {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   373
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   374
\path[->] (1) edge node [above]  {$\epsilon$} (2);
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   375
\path[->] (1) edge node [below]  {$\epsilon$} (3);
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   376
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   377
\begin{pgfonlayer}{background}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   378
\node (3) [rounded corners, inner sep=1mm, thick, draw=black!60, fill=black!20, fit= (1) (a2) (a3) (b2) (b3)] {};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   379
\node [yshift=3mm] at (3.north) {$r_1+ r_2$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   380
\end{pgfonlayer}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   381
\end{tikzpicture}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   382
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   383
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   384
\noindent 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   385
Finally for the $*$-case we have an automaton for $r$
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   386
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   387
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   388
\begin{tikzpicture}[node distance=3mm,
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   389
                             >=stealth',very thick, every state/.style={minimum size=3pt,draw=blue!50,very thick,fill=blue!20},]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   390
\node at (0,0)  (1)  {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   391
\node[state, initial]  (2)  [right=16mm of 1] {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   392
\node (a)  [right=of 2] {$\ldots$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   393
\node[state, accepting]  (a1)  [right=of a] {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   394
\node[state, accepting]  (a2)  [above=of a1] {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   395
\node[state, accepting]  (a3)  [below=of a1] {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   396
\begin{pgfonlayer}{background}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   397
\node (1) [rounded corners, inner sep=1mm, thick, draw=black!60, fill=black!20, fit= (2) (a1) (a2) (a3)] {};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   398
\node [yshift=3mm] at (1.north) {$r$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   399
\end{pgfonlayer}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   400
\end{tikzpicture}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   401
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   402
251
5b5a68df6d16 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 217
diff changeset
   403
\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
   404
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
   405
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
   406
empty string. This gives the following automaton for $r^*$:
143
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   407
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   408
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   409
\begin{tikzpicture}[node distance=3mm,
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   410
                             >=stealth',very thick, every state/.style={minimum size=3pt,draw=blue!50,very thick,fill=blue!20},]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   411
\node at (0,0) [state, initial,accepting]  (1)  {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   412
\node[state]  (2)  [right=16mm of 1] {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   413
\node (a)  [right=of 2] {$\ldots$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   414
\node[state]  (a1)  [right=of a] {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   415
\node[state]  (a2)  [above=of a1] {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   416
\node[state]  (a3)  [below=of a1] {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   417
\path[->] (1) edge node [above]  {$\epsilon$} (2);
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   418
\path[->] (a1) edge [bend left=45] node [above]  {$\epsilon$} (1);
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   419
\path[->] (a2) edge [bend right] node [below]  {$\epsilon$} (1);
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   420
\path[->] (a3) edge [bend left=45] node [below]  {$\epsilon$} (1);
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   421
\begin{pgfonlayer}{background}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   422
\node (2) [rounded corners, inner sep=1mm, thick, draw=black!60, fill=black!20, fit= (1) (a2) (a3)] {};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   423
\node [yshift=3mm] at (2.north) {$r^*$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   424
\end{pgfonlayer}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   425
\end{tikzpicture}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   426
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   427
251
5b5a68df6d16 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 217
diff changeset
   428
\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
   429
was invented by Ken Thompson in 1968.
143
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   430
268
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
   431
269
83e6cb90216d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 268
diff changeset
   432
\subsubsection*{Subset Construction}
268
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
   433
292
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   434
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
   435
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
   436
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
   437
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
   438
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
   439
\begin{center}
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
   440
\begin{tabular}{c@{\hspace{10mm}}c}
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
   441
\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
   442
                    every state/.style={minimum size=0pt,
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
   443
                    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
   444
                    baseline=0mm]
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
   445
\node[state,initial]  (q_0)  {$0$};
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
   446
\node[state] (q_1) [above=of q_0] {$1$};
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
   447
\node[state, accepting] (q_2) [below=of q_0] {$2$};
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
   448
\path[->] (q_0) edge node [left]  {$\epsilon$} (q_1);
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
   449
\path[->] (q_0) edge node [left]  {$\epsilon$} (q_2);
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
   450
\path[->] (q_0) edge [loop right] node  {$a$} ();
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
   451
\path[->] (q_1) edge [loop above] node  {$a$} ();
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
   452
\path[->] (q_2) edge [loop below] node  {$b$} ();
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
   453
\end{tikzpicture}
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
   454
&
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
   455
\begin{tabular}{r|cl}
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
   456
nodes & $a$ & $b$\\
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
   457
\hline
344
408fd5994288 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
   458
$\{\}\phantom{\star}$ & $\{\}$ & $\{\}$\\
268
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
   459
$\{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
   460
$\{1\}\phantom{\star}$       & $\{1\}$       & $\{\}$\\
408fd5994288 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
   461
$\{2\}\star$  & $\{\}$ & $\{2\}$\\
268
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
   462
$\{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
   463
$\{0,2\}\star$ & $\{0,1,2\}$   & $\{2\}$\\
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
   464
$\{1,2\}\star$ & $\{1\}$       & $\{2\}$\\
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
   465
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
   466
\end{tabular}
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
   467
\end{tabular}
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
   468
\end{center}
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
   469
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
   470
\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
   471
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
   472
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
   473
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
   474
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
   475
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
   476
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
   477
\begin{itemize}
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
   478
\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
   479
      $a$ and the node $\{0\}$
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
   480
\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
   481
\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
   482
      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
   483
\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
   484
      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
   485
      $a$-transition
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
   486
\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
   487
      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
   488
\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
   489
      when given an $a$ 
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
   490
\end{itemize}
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
   491
344
408fd5994288 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
   492
\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
   493
$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
   494
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
   495
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
   496
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
   497
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
   498
$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
   499
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
   500
$\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
   501
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
   502
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
   503
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
   504
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
   505
above is
408fd5994288 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
   506
408fd5994288 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
   507
\begin{center}
408fd5994288 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
   508
\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
   509
                    every state/.style={minimum size=0pt,
408fd5994288 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
   510
                    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
   511
                    baseline=0mm]
408fd5994288 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
   512
\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
   513
\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
   514
\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
   515
\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
   516
\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
   517
\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
   518
\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
   519
\node[state] (qn) [right=of q1] {$\{\}$};
408fd5994288 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
   520
408fd5994288 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
   521
\path[->] (q012) edge [loop below] node  {$a$} ();
408fd5994288 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
   522
\path[->] (q012) edge node [above]  {$b$} (q2);
408fd5994288 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
   523
\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
   524
\path[->] (q12) edge node [below]  {$b$} (q2);
408fd5994288 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
   525
\path[->] (q02) edge node [above]  {$a$} (q012);
408fd5994288 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
   526
\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
   527
\path[->] (q01) edge node [below]  {$a$} (q012);
408fd5994288 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
   528
\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
   529
\path[->] (q0) edge node [below]  {$a$} (q012);
408fd5994288 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
   530
\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
   531
\path[->] (q1) edge [loop above] node  {$a$} ();
408fd5994288 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
   532
\path[->] (q1) edge node [above]  {$b$} (qn);
408fd5994288 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
   533
\path[->] (q2) edge [loop right] node  {$b$} ();
408fd5994288 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
   534
\path[->] (q2) edge node [below]  {$a$} (qn);
408fd5994288 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
   535
\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
   536
\end{tikzpicture}
408fd5994288 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
   537
\end{center}
408fd5994288 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
   538
408fd5994288 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
   539
268
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
   540
292
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   541
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
   542
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
   543
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
   544
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
   545
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
   546
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
   547
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
   548
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
   549
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
   550
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
   551
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
   552
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
   553
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
   554
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
   555
344
408fd5994288 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
   556
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
   557
gives a much more legible automaton, namely
408fd5994288 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
   558
408fd5994288 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
   559
\begin{center}
408fd5994288 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
   560
\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
   561
                    every state/.style={minimum size=0pt,
408fd5994288 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
   562
                    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
   563
                    baseline=0mm]
408fd5994288 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
   564
\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
   565
\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
   566
\node[state] (qn) [right=of q2] {$\{\}$};
408fd5994288 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
   567
408fd5994288 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
   568
\path[->] (q012) edge [loop below] node  {$a$} ();
408fd5994288 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
   569
\path[->] (q012) edge node [above]  {$b$} (q2);
408fd5994288 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
   570
\path[->] (q2) edge [loop below] node  {$b$} ();
408fd5994288 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
   571
\path[->] (q2) edge node [below]  {$a$} (qn);
408fd5994288 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
   572
\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
   573
\end{tikzpicture}
408fd5994288 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
   574
\end{center}
408fd5994288 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
   575
408fd5994288 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
   576
\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
   577
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
   578
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
   579
269
83e6cb90216d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 268
diff changeset
   580
\subsubsection*{Brzozowski's Method}
83e6cb90216d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 268
diff changeset
   581
292
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   582
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
   583
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
   584
a regular expression using familiar transformations for
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   585
solving equational systems. Consider the DFA:
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   586
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   587
\begin{center}
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   588
\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
   589
                    every state/.style={minimum size=0pt,
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   590
                    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
   591
                    fill=blue!20}]
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   592
  \node[state, initial]        (q0) at ( 0,1) {$q_0$};
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   593
  \node[state]                    (q1) at ( 1,1) {$q_1$};
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   594
  \node[state, accepting] (q2) at ( 2,1) {$q_2$};
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   595
  \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
   596
            (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
   597
            (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
   598
            (q1) edge node[above] {$a$} (q2)
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   599
            (q2) edge [loop right] node {$a$} ()
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   600
            (q0) edge [loop below] node {$b$} ();
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   601
\end{tikzpicture}
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   602
\end{center}
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   603
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   604
\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
   605
system
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   606
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   607
\begin{eqnarray}
444
3056a4c071b0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 349
diff changeset
   608
q_0 & = & \ONE + q_0\,b + q_1\,b +  q_2\,b\\
292
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   609
q_1 & = & q_0\,a\\
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   610
q_2 & = & q_1\,a + q_2\,a
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   611
\end{eqnarray}
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   612
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   613
\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
   614
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
   615
constructed. First have a look at the second equation: the
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   616
left-hand side is $q_1$ and the right-hand side $q_0\,a$. The
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   617
right-hand side is essentially all possible ways how to end up
459
780486571e38 updated
Christian Urban <urbanc@in.tum.de>
parents: 444
diff changeset
   618
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
   619
an $a$.  Therefore the right hand side is this
292
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   620
state followed by character---in this case $q_0\,a$. Now lets
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   621
have a look at the third equation: there are two incoming
322
698ed1c96cd0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 318
diff changeset
   622
edges for $q_2$. Therefore we have two terms, namely $q_1\,a$ and
292
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   623
$q_2\,a$. These terms are separated by $+$. The first states
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   624
that if in state $q_1$ consuming an $a$ will bring you to
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   625
$q_2$, and the secont that being in $q_2$ and consuming an $a$
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   626
will make you stay in $q_2$. The right-hand side of the
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   627
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
   628
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
   629
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
   630
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
   631
in the DFA.
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   632
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   633
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
   634
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
   635
solving usual linear equational systems. For example the
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   636
second equation does not contain the variable $q_1$ on the
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   637
right-hand side of the equation. We can therefore eliminate 
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   638
$q_1$ from the system by just substituting this equation
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   639
into the other two. This gives
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   640
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   641
\begin{eqnarray}
444
3056a4c071b0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 349
diff changeset
   642
q_0 & = & \ONE + q_0\,b + q_0\,a\,b +  q_2\,b\\
292
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   643
q_2 & = & q_0\,a\,a + q_2\,a
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   644
\end{eqnarray}
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   645
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   646
\noindent where in Equation (4) we have two occurences
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   647
of $q_0$. Like the laws about $+$ and $\cdot$, we can simplify 
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   648
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
   649
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   650
\begin{eqnarray}
444
3056a4c071b0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 349
diff changeset
   651
q_0 & = & \ONE + q_0\,(b + a\,b) +  q_2\,b\\
292
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   652
q_2 & = & q_0\,a\,a + q_2\,a
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   653
\end{eqnarray}
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   654
 
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   655
\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
   656
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
   657
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
   658
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
   659
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
   660
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
   661
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
   662
assume $+$ is symmetric, Equation (7) is of that form: $s$ is
434891622131 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 344
diff changeset
   663
$q_0\,a\,a$ and $r$ is $a$. That means we can transform
434891622131 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 344
diff changeset
   664
(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
   665
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   666
\begin{eqnarray}
444
3056a4c071b0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 349
diff changeset
   667
q_0 & = & \ONE + q_0\,(b + a\,b) +  q_2\,b\\
292
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   668
q_2 & = & q_0\,a\,a\,(a^*)
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   669
\end{eqnarray}
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   670
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   671
\noindent Now again we can substitute the second equation into
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   672
the first in order to eliminate the variable $q_2$.
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   673
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   674
\begin{eqnarray}
444
3056a4c071b0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 349
diff changeset
   675
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
   676
\end{eqnarray}
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   677
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   678
\noindent Pulling $q_0$ out as a single factor gives:
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   679
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   680
\begin{eqnarray}
444
3056a4c071b0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 349
diff changeset
   681
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
   682
\end{eqnarray}
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   683
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   684
\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
   685
apply Arden's rule ($r$ is $b + a\,b + a\,a\,(a^*)\,b$ and $s$
444
3056a4c071b0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 349
diff changeset
   686
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
   687
regular expression:
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   688
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   689
\begin{eqnarray}
444
3056a4c071b0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 349
diff changeset
   690
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
   691
\end{eqnarray}
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   692
349
434891622131 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 344
diff changeset
   693
\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
   694
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
   695
expression
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   696
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   697
\begin{eqnarray}
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   698
q_0 & = & (b + a\,b + a\,a\,(a^*)\,b)^*
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   699
\end{eqnarray}
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   700
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   701
\noindent 
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   702
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
   703
for the other equations. This gives:
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
\begin{eqnarray}
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   706
q_0 & = & (b + a\,b + a\,a\,(a^*)\,b)^*\\
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   707
q_1 & = & (b + a\,b + a\,a\,(a^*)\,b)^*\,a\\
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   708
q_2 & = & (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
   709
\end{eqnarray}
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   710
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   711
\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
   712
which correspond to a terminal state. In our running example,
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   713
this is just $q_2$. Consequently, a regular expression
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   714
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
   715
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   716
\[
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   717
(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
   718
\]
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   719
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   720
\noindent You can somewhat crosscheck your solution
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   721
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
   722
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
   723
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
   724
string is also matched by the automaton.
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   725
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   726
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
   727
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
   728
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
   729
83e6cb90216d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 268
diff changeset
   730
\subsubsection*{Automata Minimization}
83e6cb90216d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 268
diff changeset
   731
270
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   732
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
   733
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
   734
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
   735
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
   736
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   737
\begin{enumerate}
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   738
\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
   739
\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
   740
\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
   741
      test whether 
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   742
      
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   743
      \begin{center} 
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   744
      $(\delta(q, c), \delta(p,c))$ 
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   745
      \end{center} 
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   746
      
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   747
      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
   748
\item Repeat last step until no change.
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   749
\item All unmarked pairs can be merged.
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   750
\end{enumerate}
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   751
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   752
\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
   753
DFA.
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   754
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   755
\begin{center}
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   756
\begin{tikzpicture}[>=stealth',very thick,auto,
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   757
                    every state/.style={minimum size=0pt,
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   758
                    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
   759
                    fill=blue!20}]
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   760
\node[state,initial]  (q_0)  {$q_0$};
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   761
\node[state] (q_1) [right=of q_0] {$q_1$};
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   762
\node[state] (q_2) [below right=of q_0] {$q_2$};
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   763
\node[state] (q_3) [right=of q_2] {$q_3$};
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   764
\node[state, accepting] (q_4) [right=of q_1] {$q_4$};
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   765
\path[->] (q_0) edge node [above]  {$a$} (q_1);
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   766
\path[->] (q_1) edge node [above]  {$a$} (q_4);
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   767
\path[->] (q_4) edge [loop right] node  {$a, b$} ();
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   768
\path[->] (q_3) edge node [right]  {$a$} (q_4);
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   769
\path[->] (q_2) edge node [above]  {$a$} (q_3);
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   770
\path[->] (q_1) edge node [right]  {$b$} (q_2);
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   771
\path[->] (q_0) edge node [above]  {$b$} (q_2);
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   772
\path[->] (q_2) edge [loop left] node  {$b$} ();
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   773
\path[->] (q_3) edge [bend left=95, looseness=1.3] node 
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   774
  [below]  {$b$} (q_0);
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   775
\end{tikzpicture}
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   776
\end{center}
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   777
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   778
\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
   779
of the form
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   780
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   781
\begin{center}
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   782
\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
   783
\draw (0,0) -- (4,0);
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   784
\draw (0,1) -- (4,1);
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   785
\draw (0,2) -- (3,2);
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   786
\draw (0,3) -- (2,3);
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   787
\draw (0,4) -- (1,4);
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   788
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   789
\draw (0,0) -- (0, 4);
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   790
\draw (1,0) -- (1, 4);
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   791
\draw (2,0) -- (2, 3);
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   792
\draw (3,0) -- (3, 2);
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   793
\draw (4,0) -- (4, 1);
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   794
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   795
\draw (0.5,-0.5) node {$q_0$}; 
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   796
\draw (1.5,-0.5) node {$q_1$}; 
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   797
\draw (2.5,-0.5) node {$q_2$}; 
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   798
\draw (3.5,-0.5) node {$q_3$};
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   799
 
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   800
\draw (-0.5, 3.5) node {$q_1$}; 
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   801
\draw (-0.5, 2.5) node {$q_2$}; 
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   802
\draw (-0.5, 1.5) node {$q_3$}; 
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   803
\draw (-0.5, 0.5) node {$q_4$}; 
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
\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
   806
\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
   807
\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
   808
\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
   809
\end{tikzpicture}
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   810
\end{center}
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   811
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   812
\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
   813
the corresponding pairs there is always one state that is
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   814
accepting ($q_4$) and a state that is non-accepting (the other
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   815
states).
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   816
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   817
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
   818
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
   819
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
   820
This gives the triangle
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   821
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   822
\begin{center}
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   823
\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
   824
\draw (0,0) -- (4,0);
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   825
\draw (0,1) -- (4,1);
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   826
\draw (0,2) -- (3,2);
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   827
\draw (0,3) -- (2,3);
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   828
\draw (0,4) -- (1,4);
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   829
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   830
\draw (0,0) -- (0, 4);
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   831
\draw (1,0) -- (1, 4);
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   832
\draw (2,0) -- (2, 3);
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   833
\draw (3,0) -- (3, 2);
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   834
\draw (4,0) -- (4, 1);
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   835
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   836
\draw (0.5,-0.5) node {$q_0$}; 
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   837
\draw (1.5,-0.5) node {$q_1$}; 
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   838
\draw (2.5,-0.5) node {$q_2$}; 
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   839
\draw (3.5,-0.5) node {$q_3$};
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   840
 
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   841
\draw (-0.5, 3.5) node {$q_1$}; 
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   842
\draw (-0.5, 2.5) node {$q_2$}; 
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   843
\draw (-0.5, 1.5) node {$q_3$}; 
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   844
\draw (-0.5, 0.5) node {$q_4$}; 
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   845
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   846
\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
   847
\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
   848
\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
   849
\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
   850
\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
   851
\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
   852
\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
   853
\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
   854
\end{tikzpicture}
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   855
\end{center}
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   856
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   857
\noindent which means states $q_0$ and $q_2$, as well as $q_1$
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   858
and $q_3$ can be merged. This gives the following minimal DFA
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   859
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   860
\begin{center}
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   861
\begin{tikzpicture}[>=stealth',very thick,auto,
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   862
                    every state/.style={minimum size=0pt,
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   863
                    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
   864
                    fill=blue!20}]
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   865
\node[state,initial]  (q_02)  {$q_{0, 2}$};
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   866
\node[state] (q_13) [right=of q_02] {$q_{1, 3}$};
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   867
\node[state, accepting] (q_4) [right=of q_13] 
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   868
  {$q_{4\phantom{,0}}$};
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   869
\path[->] (q_02) edge [bend left] node [above]  {$a$} (q_13);
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   870
\path[->] (q_13) edge [bend left] node [below]  {$b$} (q_02);
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   871
\path[->] (q_02) edge [loop below] node  {$b$} ();
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   872
\path[->] (q_13) edge node [above]  {$a$} (q_4);
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   873
\path[->] (q_4) edge [loop above] node  {$a, b$} ();
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   874
\end{tikzpicture}
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   875
\end{center}
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   876
269
83e6cb90216d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 268
diff changeset
   877
\subsubsection*{Regular Languages}
83e6cb90216d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 268
diff changeset
   878
83e6cb90216d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 268
diff changeset
   879
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
   880
the following overall picture:
269
83e6cb90216d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 268
diff changeset
   881
83e6cb90216d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 268
diff changeset
   882
\begin{center}
83e6cb90216d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 268
diff changeset
   883
\begin{tikzpicture}
83e6cb90216d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 268
diff changeset
   884
\node (rexp)  {\bf Regexps};
83e6cb90216d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 268
diff changeset
   885
\node (nfa) [right=of rexp] {\bf NFAs};
83e6cb90216d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 268
diff changeset
   886
\node (dfa) [right=of nfa] {\bf DFAs};
83e6cb90216d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 268
diff changeset
   887
\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
   888
\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
   889
\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
   890
\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
   891
\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
   892
\end{tikzpicture}
83e6cb90216d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 268
diff changeset
   893
\end{center}
83e6cb90216d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 268
diff changeset
   894
83e6cb90216d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 268
diff changeset
   895
\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
   896
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
   897
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
   898
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
   899
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
   900
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
   901
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
   902
83e6cb90216d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 268
diff changeset
   903
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
   904
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
   905
83e6cb90216d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 268
diff changeset
   906
\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
   907
regular expression that recognises all its strings.
83e6cb90216d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 268
diff changeset
   908
\end{quote}
83e6cb90216d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 268
diff changeset
   909
83e6cb90216d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 268
diff changeset
   910
\noindent or equivalently 
83e6cb90216d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 268
diff changeset
   911
83e6cb90216d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 268
diff changeset
   912
\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
   913
automaton that recognises all its strings.
83e6cb90216d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 268
diff changeset
   914
\end{quote}
268
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
   915
269
83e6cb90216d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 268
diff changeset
   916
\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
   917
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
   918
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
   919
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
   920
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
   921
$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
   922
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
   923
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
   924
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
   925
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
   926
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
   927
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
   928
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
   929
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
   930
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
   931
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
   932
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
   933
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
   934
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
   935
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
   936
penalty.  
268
18bef085a7ca updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 251
diff changeset
   937
269
83e6cb90216d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 268
diff changeset
   938
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
   939
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
   940
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
   941
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
   942
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
   943
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
   944
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
   945
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
   946
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
   947
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
   948
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
   949
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
   950
runtime penalty.
269
83e6cb90216d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 268
diff changeset
   951
83e6cb90216d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 268
diff changeset
   952
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
   953
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
   954
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
   955
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
   956
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
   957
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
   958
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
   959
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
   960
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
   961
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
   962
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
   963
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
   964
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
   965
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
   966
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
   967
349
434891622131 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 344
diff changeset
   968
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
   969
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
   970
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
   971
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   972
\[a^n\,b^n\]
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   973
7ed2a25dd115 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 270
diff changeset
   974
\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
   975
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
   976
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
   977
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
   978
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
   979
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
   980
344
408fd5994288 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 333
diff changeset
   981
\section*{Further Reading}
270
4dbeaf43031d updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 269
diff changeset
   982
471
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 459
diff changeset
   983
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
   984
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
   985
algorithm generates.
325
794c599cee53 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 324
diff changeset
   986
794c599cee53 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 324
diff changeset
   987
%http://www.inf.ed.ac.uk/teaching/courses/ct/
140
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   988
\end{document}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   989
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   990
%%% Local Variables: 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   991
%%% mode: latex
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   992
%%% TeX-master: t
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   993
%%% End: