handouts/ho03.tex
author Christian Urban <christian dot urban at kcl dot ac dot uk>
Sun, 27 Oct 2013 01:07:25 +0100
changeset 162 edcd84c7b491
parent 144 0cb61bed557d
child 217 cd6066f1056a
permissions -rw-r--r--
added
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}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     2
\usepackage{charter}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     3
\usepackage{hyperref}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     4
\usepackage{amssymb}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     5
\usepackage{amsmath}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     6
\usepackage[T1]{fontenc}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     7
\usepackage{listings}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
     8
\usepackage{xcolor}
142
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 141
diff changeset
     9
\usepackage{tikz}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 141
diff changeset
    10
\usetikzlibrary{arrows}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 141
diff changeset
    11
\usetikzlibrary{automata}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 141
diff changeset
    12
\usetikzlibrary{shapes}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 141
diff changeset
    13
\usetikzlibrary{shadows}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 141
diff changeset
    14
\usetikzlibrary{positioning}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 141
diff changeset
    15
\usetikzlibrary{calc}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 141
diff changeset
    16
\usetikzlibrary{fit}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 141
diff changeset
    17
\usetikzlibrary{backgrounds}
140
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    18
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    19
\newcommand{\dn}{\stackrel{\mbox{\scriptsize def}}{=}}%
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    20
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    21
\definecolor{javared}{rgb}{0.6,0,0} % for strings
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    22
\definecolor{javagreen}{rgb}{0.25,0.5,0.35} % comments
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    23
\definecolor{javapurple}{rgb}{0.5,0,0.35} % keywords
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    24
\definecolor{javadocblue}{rgb}{0.25,0.35,0.75} % javadoc
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    25
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    26
\lstdefinelanguage{scala}{
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    27
  morekeywords={abstract,case,catch,class,def,%
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    28
    do,else,extends,false,final,finally,%
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    29
    for,if,implicit,import,match,mixin,%
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    30
    new,null,object,override,package,%
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    31
    private,protected,requires,return,sealed,%
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    32
    super,this,throw,trait,true,try,%
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    33
    type,val,var,while,with,yield},
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    34
  otherkeywords={=>,<-,<\%,<:,>:,\#,@},
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    35
  sensitive=true,
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    36
  morecomment=[l]{//},
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    37
  morecomment=[n]{/*}{*/},
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    38
  morestring=[b]",
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    39
  morestring=[b]',
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    40
  morestring=[b]"""
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    41
}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    42
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    43
\lstset{language=Scala,
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    44
	basicstyle=\ttfamily,
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    45
	keywordstyle=\color{javapurple}\bfseries,
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    46
	stringstyle=\color{javagreen},
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    47
	commentstyle=\color{javagreen},
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    48
	morecomment=[s][\color{javadocblue}]{/**}{*/},
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    49
	numbers=left,
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    50
	numberstyle=\tiny\color{black},
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    51
	stepnumber=1,
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    52
	numbersep=10pt,
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    53
	tabsize=2,
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    54
	showspaces=false,
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    55
	showstringspaces=false}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    56
	
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    57
\begin{document}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    58
141
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 140
diff changeset
    59
\section*{Handout 3}
140
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    60
142
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 141
diff changeset
    61
Let us have a closer look at automata and their relation to regular expressions. This will help us to understand
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 141
diff changeset
    62
why the regular expression matchers in Python and Ruby are so slow with certain regular expressions. 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 141
diff changeset
    63
143
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
    64
A \emph{deterministic finite automaton} (DFA), say $A$, is defined 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
    65
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 141
diff changeset
    66
\begin{itemize}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 141
diff changeset
    67
\item $Q$ is a set of states,
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 141
diff changeset
    68
\item $q_0 \in Q$ is the start state,
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 141
diff changeset
    69
\item $F \subseteq Q$ are the accepting states, and
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 141
diff changeset
    70
\item $\delta$ is the transition function.
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 141
diff changeset
    71
\end{itemize}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 141
diff changeset
    72
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 141
diff changeset
    73
\noindent
143
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
    74
The transition function determines how to ``transition'' from one state to the next state with respect to a character.
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
    75
We have the assumption that these functions do not need to be defined everywhere: so it can be the case that
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
    76
given a character there is no next state, in which case we need to raise a kind of ``raise an exception''.  A typical 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
    77
example of a DFA is
142
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 141
diff changeset
    78
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 141
diff changeset
    79
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 141
diff changeset
    80
\begin{tikzpicture}[>=stealth',very thick,auto,
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 141
diff changeset
    81
                             every state/.style={minimum size=0pt,inner sep=2pt,draw=blue!50,very thick,fill=blue!20},]
143
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
    82
\node[state,initial]  (q_0)  {$q_0$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
    83
\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
    84
\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
    85
\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
    86
\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
    87
\path[->] (q_0) edge node [above]  {$a$} (q_1);
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 141
diff changeset
    88
\path[->] (q_1) edge node [above]  {$a$} (q_4);
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 141
diff changeset
    89
\path[->] (q_4) edge [loop right] node  {$a, b$} ();
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 141
diff changeset
    90
\path[->] (q_3) edge node [right]  {$a$} (q_4);
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 141
diff changeset
    91
\path[->] (q_2) edge node [above]  {$a$} (q_3);
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 141
diff changeset
    92
\path[->] (q_1) edge node [right]  {$b$} (q_2);
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 141
diff changeset
    93
\path[->] (q_0) edge node [above]  {$b$} (q_2);
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 141
diff changeset
    94
\path[->] (q_2) edge [loop left] node  {$b$} ();
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 141
diff changeset
    95
\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
    96
\end{tikzpicture}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 141
diff changeset
    97
\end{center}
140
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
    98
143
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
    99
\noindent
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   100
The accepting state $q_4$ is indicated with double circles. It is possible that a DFA has no
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   101
accepting states at all, or that the starting state is also an accepting state.
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   102
In the case above the transition function is defined everywhere and can be given as a table
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   103
as follows:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   104
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   105
\[
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   106
\begin{array}{lcl}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   107
(q_0, a) &\rightarrow& q_1\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   108
(q_0, b) &\rightarrow& q_2\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   109
(q_1, a) &\rightarrow& q_4\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   110
(q_1, b) &\rightarrow& q_2\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   111
(q_2, a) &\rightarrow& q_3\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   112
(q_2, b) &\rightarrow& q_2\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   113
(q_3, a) &\rightarrow& q_4\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   114
(q_3, b) &\rightarrow& q_0\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   115
(q_4, a) &\rightarrow& q_4\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   116
(q_4, b) &\rightarrow& q_4\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   117
\end{array}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   118
\]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   119
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   120
\noindent
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   121
We need to define the notion of what language is accepted by an automaton. For this we 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   122
lift the transition function $\delta$ from characters to strings as follows:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   123
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   124
\[
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   125
\begin{array}{lcl}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   126
\hat{\delta}(q, "")        & \dn & q\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   127
\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
   128
\end{array}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   129
\]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   130
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   131
\noindent
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   132
Given a string this means we start in the starting state and take the first character of the string,
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   133
follow to the next sate, then take the second character and so on. Once the string is exhausted
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   134
and we end up in an accepting state, then this string is accepted. Otherwise it is not accepted. 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   135
So $s$ in the \emph{language accepted by the automaton} $A(Q, q_0, F, \delta)$ iff
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   136
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   137
\[
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   138
\hat{\delta}(q_0, s) \in F 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   139
\]
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   140
  
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   141
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   142
While with DFA it will always clear that given a character what the next state is, it will be useful to relax 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   143
this restriction. The resulting construction is called a \emph{non-deterministic finite automaton} (NFA) given
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   144
as a four-tuple $A(Q, q_0, F, \rho)$ where
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   145
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   146
\begin{itemize}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   147
\item $Q$ is a finite set of states
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   148
\item $q_0$ is a start state
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   149
\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
   150
\item $\rho$ is a transition relation.
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   151
\end{itemize}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   152
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   153
\noindent
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   154
Two typical examples of NFAs are
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   155
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   156
\begin{tabular}[t]{c@{\hspace{9mm}}c}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   157
\begin{tikzpicture}[scale=0.7,>=stealth',very thick,
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   158
                             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
   159
\node[state,initial]  (q_0)  {$q_0$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   160
\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
   161
\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
   162
\path[->] (q_0) edge node [left]  {$\epsilon$} (q_1);
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   163
\path[->] (q_0) edge node [left]  {$\epsilon$} (q_2);
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   164
\path[->] (q_0) edge [loop right] node  {$a$} ();
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   165
\path[->] (q_1) edge [loop above] node  {$a$} ();
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   166
\path[->] (q_2) edge [loop below] node  {$b$} ();
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   167
\end{tikzpicture} &
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   168
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   169
\raisebox{20mm}{
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   170
\begin{tikzpicture}[scale=0.7,>=stealth',very thick,
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   171
                             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
   172
\node[state,initial]  (r_1)  {$r_1$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   173
\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
   174
\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
   175
\path[->] (r_1) edge node [below]  {$b$} (r_3);
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   176
\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
   177
\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
   178
\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
   179
\end{tikzpicture}}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   180
\end{tabular}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   181
\end{center}
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
\noindent
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   184
There are a number of points you should note. Every DFA is a NFA, but not vice versa.
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   185
The $\rho$ in NFAs is a transition \emph{relation} 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   186
(DFAs have a transition function). The difference between a function and a relation is that a function 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   187
has always a single output, while a relation gives, roughly speaking, several outputs. Look
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   188
at the NFA on the right-hand side above: if you are currently in the state $r_2$ and you read a
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   189
character $a$, then you can transition to $r_1$ \emph{or} $r_3$. Which route you take is not
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   190
determined. This means if we need to decide whether a string is accepted by a NFA, we might have 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   191
to explore all possibilities. Also there is a special transition in NFAs which is called \emph{epsilon-transition}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   192
or \emph{silent transition}. This transition means you do not have to ``consume'' no part of
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   193
the input string, but ``silently'' change to a different state.
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   194
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   195
The reason for introducing NFAs is that there is a relatively simple (recursive) translation of regular expressions into
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   196
NFAs. Consider the simple regular expressions $\varnothing$, $\epsilon$ and $c$. They can be translated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   197
as follows:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   198
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   199
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   200
\begin{tabular}[t]{l@{\hspace{10mm}}l}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   201
\raisebox{1mm}{$\varnothing$} & 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   202
\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
   203
\node[state, initial]  (q_0)  {$\mbox{}$};
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
\raisebox{1mm}{$\epsilon$} & 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   206
\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
   207
\node[state, initial, accepting]  (q_0)  {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   208
\end{tikzpicture}\\\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   209
\raisebox{2mm}{$c$} & 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   210
\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
   211
\node[state, initial]  (q_0)  {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   212
\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
   213
\path[->] (q_0) edge node [below]  {$c$} (q_1);
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   214
\end{tikzpicture}\\\\
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   215
\end{tabular}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   216
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   217
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   218
\noindent
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   219
The case for the sequence regular expression $r_1 \cdot r_2$ is as follows: We are given by recursion
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   220
two automata representing $r_1$ and $r_2$ respectively. 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   221
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   222
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   223
\begin{tikzpicture}[node distance=3mm,
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   224
                             >=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
   225
\node[state, initial]  (q_0)  {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   226
\node (r_1)  [right=of q_0] {$\ldots$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   227
\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
   228
\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
   229
\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
   230
\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
   231
\node (b_1)  [right=of a_0] {$\ldots$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   232
\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
   233
\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
   234
\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
   235
\begin{pgfonlayer}{background}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   236
\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
   237
\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
   238
\node [yshift=2mm] at (1.north) {$r_1$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   239
\node [yshift=2mm] at (2.north) {$r_2$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   240
\end{pgfonlayer}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   241
\end{tikzpicture}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   242
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   243
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   244
\noindent
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   245
The first automaton has some accepting states. We obtain an automaton for $r_1\cdot r_2$ by connecting
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   246
these accepting states with $\epsilon$-transitions to the starting state of the second automaton. By doing 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   247
so we make them non-accepting like so:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   248
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   249
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   250
\begin{tikzpicture}[node distance=3mm,
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   251
                             >=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
   252
\node[state, initial]  (q_0)  {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   253
\node (r_1)  [right=of q_0] {$\ldots$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   254
\node[state]  (t_1)  [right=of r_1] {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   255
\node[state]  (t_2)  [above=of t_1] {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   256
\node[state]  (t_3)  [below=of t_1] {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   257
\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
   258
\node (b_1)  [right=of a_0] {$\ldots$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   259
\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
   260
\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
   261
\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
   262
\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
   263
\path[->] (t_2) edge node [above]  {$\epsilon$} (a_0);
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   264
\path[->] (t_3) edge node [below]  {$\epsilon$} (a_0);
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   265
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   266
\begin{pgfonlayer}{background}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   267
\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
   268
\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
   269
\end{pgfonlayer}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   270
\end{tikzpicture}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   271
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   272
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   273
\noindent
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   274
The case for the choice regular expression $r_1 + r_2$ is slightly different: We are given by recursion
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   275
two automata representing $r_1$ and $r_2$ respectively. 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   276
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   277
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   278
\begin{tikzpicture}[node distance=3mm,
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   279
                             >=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
   280
\node at (0,0)  (1)  {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   281
\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
   282
\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
   283
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   284
\node (a)  [right=of 2] {$\ldots$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   285
\node[state, accepting]  (a1)  [right=of a] {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   286
\node[state, accepting]  (a2)  [above=of a1] {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   287
\node[state, accepting]  (a3)  [below=of a1] {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   288
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   289
\node (b)  [right=of 3] {$\ldots$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   290
\node[state, accepting]  (b1)  [right=of b] {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   291
\node[state, accepting]  (b2)  [above=of b1] {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   292
\node[state, accepting]  (b3)  [below=of b1] {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   293
\begin{pgfonlayer}{background}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   294
\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
   295
\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
   296
\node [yshift=3mm] at (1.north) {$r_1$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   297
\node [yshift=3mm] at (2.north) {$r_2$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   298
\end{pgfonlayer}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   299
\end{tikzpicture}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   300
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   301
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   302
\noindent
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   303
Each automaton has a single start state and potentially several accepting states. We obtain a
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   304
NFA for the regular expression $r_1 + r_2$ by introducing a new starting state and connecting it
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   305
with an $\epsilon$-transition to the two starting states above, like so
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   306
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   307
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   308
\hspace{2cm}\begin{tikzpicture}[node distance=3mm,
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   309
                             >=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
   310
\node at (0,0) [state, initial]  (1)  {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   311
\node[state]  (2)  [above right=16mm of 1] {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   312
\node[state]  (3)  [below right=16mm of 1] {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   313
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   314
\node (a)  [right=of 2] {$\ldots$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   315
\node[state, accepting]  (a1)  [right=of a] {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   316
\node[state, accepting]  (a2)  [above=of a1] {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   317
\node[state, accepting]  (a3)  [below=of a1] {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   318
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   319
\node (b)  [right=of 3] {$\ldots$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   320
\node[state, accepting]  (b1)  [right=of b] {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   321
\node[state, accepting]  (b2)  [above=of b1] {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   322
\node[state, accepting]  (b3)  [below=of b1] {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   323
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   324
\path[->] (1) edge node [above]  {$\epsilon$} (2);
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   325
\path[->] (1) edge node [below]  {$\epsilon$} (3);
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   326
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   327
\begin{pgfonlayer}{background}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   328
\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
   329
\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
   330
\end{pgfonlayer}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   331
\end{tikzpicture}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   332
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   333
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   334
\noindent 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   335
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
   336
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   337
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   338
\begin{tikzpicture}[node distance=3mm,
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   339
                             >=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
   340
\node at (0,0)  (1)  {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   341
\node[state, initial]  (2)  [right=16mm of 1] {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   342
\node (a)  [right=of 2] {$\ldots$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   343
\node[state, accepting]  (a1)  [right=of a] {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   344
\node[state, accepting]  (a2)  [above=of a1] {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   345
\node[state, accepting]  (a3)  [below=of a1] {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   346
\begin{pgfonlayer}{background}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   347
\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
   348
\node [yshift=3mm] at (1.north) {$r$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   349
\end{pgfonlayer}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   350
\end{tikzpicture}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   351
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   352
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   353
\noindent
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   354
and connect its accepting states to a new starting state via $\epsilon$-transitions. This new
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   355
starting state is also an accepting state, because $r^*$ can also recognise the empty string.
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   356
This gives the following automaton for $r^*$:
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   357
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   358
\begin{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   359
\begin{tikzpicture}[node distance=3mm,
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   360
                             >=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
   361
\node at (0,0) [state, initial,accepting]  (1)  {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   362
\node[state]  (2)  [right=16mm of 1] {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   363
\node (a)  [right=of 2] {$\ldots$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   364
\node[state]  (a1)  [right=of a] {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   365
\node[state]  (a2)  [above=of a1] {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   366
\node[state]  (a3)  [below=of a1] {$\mbox{}$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   367
\path[->] (1) edge node [above]  {$\epsilon$} (2);
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   368
\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
   369
\path[->] (a2) edge [bend right] node [below]  {$\epsilon$} (1);
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   370
\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
   371
\begin{pgfonlayer}{background}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   372
\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
   373
\node [yshift=3mm] at (2.north) {$r^*$};
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   374
\end{pgfonlayer}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   375
\end{tikzpicture}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   376
\end{center}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   377
144
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 143
diff changeset
   378
\noindent
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 143
diff changeset
   379
This construction of a NFA from a regular expression was invented by Ken Thompson in 1968.
143
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 142
diff changeset
   380
140
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   381
\end{document}
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   382
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   383
%%% Local Variables: 
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   384
%%% mode: latex
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   385
%%% TeX-master: t
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents:
diff changeset
   386
%%% End: