| author | Christian Urban <christian.urban@kcl.ac.uk> | 
| Fri, 31 Oct 2025 11:25:14 +0000 | |
| changeset 1019 | f71399fe3fdc | 
| parent 1006 | 674d1258a2fb | 
| permissions | -rw-r--r-- | 
| 23 | 1  | 
\documentclass{article}
 | 
| 
264
 
4deef8ac5d72
uodated hws
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
258 
diff
changeset
 | 
2  | 
\usepackage{../style}
 | 
| 
 
4deef8ac5d72
uodated hws
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
258 
diff
changeset
 | 
3  | 
\usepackage{../graphics}
 | 
| 23 | 4  | 
|
5  | 
\begin{document}
 | 
|
6  | 
||
7  | 
\section*{Homework 3}
 | 
|
8  | 
||
| 916 | 9  | 
%\HEADER  | 
| 
347
 
22b5294daa2a
updated hws
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
294 
diff
changeset
 | 
10  | 
|
| 23 | 11  | 
\begin{enumerate}
 | 
| 647 | 12  | 
\item The regular expression matchers in Java, Python and Ruby can be  | 
13  | 
very slow with some (basic) regular expressions. What is the main  | 
|
14  | 
reason for this inefficient computation?  | 
|
| 892 | 15  | 
|
16  | 
  \solution{Many matchers employ DFS type of algorithms to check
 | 
|
17  | 
if a string is matched by the regex or not. Such algorithms  | 
|
18  | 
require backtracking if have gone down the wrong path which  | 
|
19  | 
can be very slow. There are also problems with bounded regular  | 
|
20  | 
expressions and backreferences.}  | 
|
| 647 | 21  | 
|
| 
401
 
5d85dc9779b1
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
355 
diff
changeset
 | 
22  | 
\item What is a regular language? Are there alternative ways  | 
| 
 
5d85dc9779b1
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
355 
diff
changeset
 | 
23  | 
to define this notion? If yes, give an explanation why  | 
| 
 
5d85dc9779b1
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
355 
diff
changeset
 | 
24  | 
they define the same notion.  | 
| 
267
 
a1544b804d1e
updated homeworks
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
264 
diff
changeset
 | 
25  | 
|
| 892 | 26  | 
      \solution{A regular language is a language for which every string
 | 
27  | 
can be recognized by some regular expression. Another definition is  | 
|
28  | 
that it is a language for which a finite automaton can be  | 
|
29  | 
constructed. Both define the same set of languages.}  | 
|
30  | 
||
| 
267
 
a1544b804d1e
updated homeworks
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
264 
diff
changeset
 | 
31  | 
\item Why is every finite set of strings a regular language?  | 
| 
132
 
04264d0c43bb
added
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
102 
diff
changeset
 | 
32  | 
|
| 892 | 33  | 
  \solution{Take a regex composed of all strings (works for finite languages)}
 | 
34  | 
||
| 
401
 
5d85dc9779b1
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
355 
diff
changeset
 | 
35  | 
\item Assume you have an alphabet consisting of the letters  | 
| 
 
5d85dc9779b1
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
355 
diff
changeset
 | 
36  | 
$a$, $b$ and $c$ only. (1) Find a regular expression  | 
| 
 
5d85dc9779b1
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
355 
diff
changeset
 | 
37  | 
that recognises the two strings $ab$ and $ac$. (2) Find  | 
| 
 
5d85dc9779b1
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
355 
diff
changeset
 | 
38  | 
a regular expression that matches all strings  | 
| 
 
5d85dc9779b1
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
355 
diff
changeset
 | 
39  | 
      \emph{except} these two strings. Note, you can only use
 | 
| 
 
5d85dc9779b1
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
355 
diff
changeset
 | 
40  | 
regular expressions of the form  | 
| 
258
 
1e4da6d2490c
updated programs
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
146 
diff
changeset
 | 
41  | 
|
| 
267
 
a1544b804d1e
updated homeworks
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
264 
diff
changeset
 | 
42  | 
  \begin{center} $r ::=
 | 
| 
401
 
5d85dc9779b1
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
355 
diff
changeset
 | 
43  | 
\ZERO \;|\; \ONE \;|\; c \;|\; r_1 + r_2 \;|\;  | 
| 
267
 
a1544b804d1e
updated homeworks
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
264 
diff
changeset
 | 
44  | 
r_1 \cdot r_2 \;|\; r^*$  | 
| 
 
a1544b804d1e
updated homeworks
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
264 
diff
changeset
 | 
45  | 
  \end{center}
 | 
| 
 
a1544b804d1e
updated homeworks
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
264 
diff
changeset
 | 
46  | 
|
| 647 | 47  | 
%\item Define the function \textit{zeroable} which takes a
 | 
48  | 
% regular expression as argument and returns a boolean.  | 
|
49  | 
% The function should satisfy the following property:  | 
|
50  | 
%  | 
|
51  | 
%  \begin{center}
 | 
|
52  | 
%    $\textit{zeroable(r)} \;\text{if and only if}\; 
 | 
|
53  | 
%    L(r) = \{\}$
 | 
|
54  | 
%  \end{center}
 | 
|
| 
267
 
a1544b804d1e
updated homeworks
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
264 
diff
changeset
 | 
55  | 
|
| 892 | 56  | 
  \solution{Done in the video but there I forgot to include the empty string.}
 | 
57  | 
||
| 
267
 
a1544b804d1e
updated homeworks
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
264 
diff
changeset
 | 
58  | 
\item Given the alphabet $\{a,b\}$. Draw the automaton that has two
 | 
| 517 | 59  | 
states, say $Q_0$ and $Q_1$. The starting state is $Q_0$ and the  | 
60  | 
final state is $Q_1$. The transition function is given by  | 
|
| 
258
 
1e4da6d2490c
updated programs
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
146 
diff
changeset
 | 
61  | 
|
| 
267
 
a1544b804d1e
updated homeworks
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
264 
diff
changeset
 | 
62  | 
  \begin{center}
 | 
| 
 
a1544b804d1e
updated homeworks
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
264 
diff
changeset
 | 
63  | 
    \begin{tabular}{l}
 | 
| 517 | 64  | 
$(Q_0, a) \rightarrow Q_0$\\  | 
65  | 
$(Q_0, b) \rightarrow Q_1$\\  | 
|
66  | 
$(Q_1, b) \rightarrow Q_1$  | 
|
| 
267
 
a1544b804d1e
updated homeworks
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
264 
diff
changeset
 | 
67  | 
    \end{tabular}
 | 
| 
 
a1544b804d1e
updated homeworks
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
264 
diff
changeset
 | 
68  | 
  \end{center}
 | 
| 
258
 
1e4da6d2490c
updated programs
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
146 
diff
changeset
 | 
69  | 
|
| 
267
 
a1544b804d1e
updated homeworks
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
264 
diff
changeset
 | 
70  | 
What is the language recognised by this automaton?  | 
| 
 
a1544b804d1e
updated homeworks
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
264 
diff
changeset
 | 
71  | 
|
| 940 | 72  | 
  \solution{
 | 
73  | 
All strings consisting of 0 or more a's then 1 or more b's,  | 
|
74  | 
which is equivalent to the language of the regular  | 
|
75  | 
expression $a^* \cdot b \cdot b^*$.  | 
|
76  | 
}  | 
|
| 936 | 77  | 
|
| 
355
 
a259eec25156
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
347 
diff
changeset
 | 
78  | 
\item Give a non-deterministic finite automaton that can  | 
| 936 | 79  | 
recognise the language $L(a\cdot (a + b)^* \cdot c)$.  | 
80  | 
||
81  | 
  \solution{It is already possible to just read off the automaton without
 | 
|
82  | 
going through Thompson.}  | 
|
| 
267
 
a1544b804d1e
updated homeworks
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
264 
diff
changeset
 | 
83  | 
|
| 517 | 84  | 
\item Given a deterministic finite automaton $A(\varSigma, Q, Q_0, F,  | 
| 
355
 
a259eec25156
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
347 
diff
changeset
 | 
85  | 
\delta)$, define which language is recognised by this  | 
| 
 
a259eec25156
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
347 
diff
changeset
 | 
86  | 
automaton. Can you define also the language defined by a  | 
| 
 
a259eec25156
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
347 
diff
changeset
 | 
87  | 
non-deterministic automaton?  | 
| 23 | 88  | 
|
| 892 | 89  | 
|
90  | 
      \solution{
 | 
|
| 1006 | 91  | 
s A formula for DFAs is  | 
92  | 
eed  | 
|
| 892 | 93  | 
        \[L(A) \dn \{s \;|\; \hat{\delta}(start_q, s) \in F\}\]
 | 
94  | 
||
95  | 
        For NFAs you need to first define what $\hat{\rho}$ means. If
 | 
|
96  | 
$\rho$ is given as a relation, you can define:  | 
|
97  | 
||
98  | 
\[  | 
|
99  | 
          \hat{\rho}(qs, []) \dn qs \qquad
 | 
|
| 942 | 100  | 
          \hat{\rho}(qs, c::s) \dn \bigcup_{q\in qs} \hat{\rho}(\{ q' \; | \; \rho(q, c, q')\}, s)
 | 
| 892 | 101  | 
\]  | 
102  | 
||
103  | 
This ``collects'' all the states reachable in a breadth-first  | 
|
104  | 
manner. Once you have all the states reachable by an NFA, you can define  | 
|
105  | 
the language as  | 
|
106  | 
||
107  | 
\[  | 
|
108  | 
        L(N) \dn \{s \;|\; \hat{\rho}(qs_{start}, s) \cap F \not= \emptyset\}
 | 
|
109  | 
\]  | 
|
110  | 
||
111  | 
Here you test whether the all states reachable (for $s$) contain at least  | 
|
112  | 
a single accepting state.  | 
|
113  | 
||
114  | 
}  | 
|
115  | 
||
| 940 | 116  | 
|
117  | 
||
| 
355
 
a259eec25156
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
347 
diff
changeset
 | 
118  | 
\item Given the following deterministic finite automaton over  | 
| 
 
a259eec25156
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
347 
diff
changeset
 | 
119  | 
      the alphabet $\{a, b\}$, find an automaton that
 | 
| 
 
a259eec25156
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
347 
diff
changeset
 | 
120  | 
recognises the complement language. (Hint: Recall that  | 
| 
 
a259eec25156
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
347 
diff
changeset
 | 
121  | 
for the algorithm from the lectures, the automaton needs  | 
| 
 
a259eec25156
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
347 
diff
changeset
 | 
122  | 
to be in completed form, that is have a transition for  | 
| 
 
a259eec25156
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
347 
diff
changeset
 | 
123  | 
every letter from the alphabet.)  | 
| 
264
 
4deef8ac5d72
uodated hws
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
258 
diff
changeset
 | 
124  | 
|
| 892 | 125  | 
      \solution{
 | 
126  | 
Before exchanging accepting and non-accepting states, it is important that  | 
|
| 939 | 127  | 
the automaton is completed (meaning has a transition for every letter  | 
| 892 | 128  | 
of the alphabet). If not completed, you have to introduce a sink state.  | 
129  | 
||
| 939 | 130  | 
For fun you can try out the example without  | 
131  | 
completion: Then the original automaton can recognise  | 
|
| 892 | 132  | 
strings of the form $a$, $ab...b$; but the ``uncompleted'' automaton would  | 
133  | 
recognise only the empty string.  | 
|
134  | 
}  | 
|
135  | 
||
| 
267
 
a1544b804d1e
updated homeworks
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
264 
diff
changeset
 | 
136  | 
  \begin{center}
 | 
| 
292
 
7ed2a25dd115
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
271 
diff
changeset
 | 
137  | 
    \begin{tikzpicture}[>=stealth',very thick,auto,
 | 
| 
 
7ed2a25dd115
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
271 
diff
changeset
 | 
138  | 
                        every state/.style={minimum size=0pt,
 | 
| 
 
7ed2a25dd115
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
271 
diff
changeset
 | 
139  | 
inner sep=2pt,draw=blue!50,very thick,  | 
| 
 
7ed2a25dd115
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
271 
diff
changeset
 | 
140  | 
fill=blue!20},scale=2]  | 
| 517 | 141  | 
      \node[state, initial]        (q0) at ( 0,1) {$Q_0$};
 | 
142  | 
      \node[state, accepting]  (q1) at ( 1,1) {$Q_1$};
 | 
|
| 
267
 
a1544b804d1e
updated homeworks
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
264 
diff
changeset
 | 
143  | 
      \path[->] (q0) edge node[above] {$a$} (q1)
 | 
| 
 
a1544b804d1e
updated homeworks
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
264 
diff
changeset
 | 
144  | 
                (q1) edge [loop right] node {$b$} ();
 | 
| 
 
a1544b804d1e
updated homeworks
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
264 
diff
changeset
 | 
145  | 
    \end{tikzpicture}
 | 
| 
 
a1544b804d1e
updated homeworks
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
264 
diff
changeset
 | 
146  | 
  \end{center}
 | 
| 
 
a1544b804d1e
updated homeworks
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
264 
diff
changeset
 | 
147  | 
|
| 
264
 
4deef8ac5d72
uodated hws
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
258 
diff
changeset
 | 
148  | 
|
| 
 
4deef8ac5d72
uodated hws
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
258 
diff
changeset
 | 
149  | 
|
| 
267
 
a1544b804d1e
updated homeworks
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
264 
diff
changeset
 | 
150  | 
%\item Given the following deterministic finite automaton  | 
| 
 
a1544b804d1e
updated homeworks
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
264 
diff
changeset
 | 
151  | 
%  | 
| 
 
a1544b804d1e
updated homeworks
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
264 
diff
changeset
 | 
152  | 
%\begin{center}
 | 
| 
 
a1544b804d1e
updated homeworks
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
264 
diff
changeset
 | 
153  | 
%\begin{tikzpicture}[scale=3, line width=0.7mm]
 | 
| 
 
a1544b804d1e
updated homeworks
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
264 
diff
changeset
 | 
154  | 
%  \node[state, initial]        (q0) at ( 0,1) {$q_0$};
 | 
| 
 
a1544b804d1e
updated homeworks
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
264 
diff
changeset
 | 
155  | 
%  \node[state,accepting]  (q1) at ( 1,1) {$q_1$};
 | 
| 
 
a1544b804d1e
updated homeworks
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
264 
diff
changeset
 | 
156  | 
%  \node[state, accepting] (q2) at ( 2,1) {$q_2$};
 | 
| 
 
a1544b804d1e
updated homeworks
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
264 
diff
changeset
 | 
157  | 
%  \path[->] (q0) edge node[above] {$b$} (q1)
 | 
| 
 
a1544b804d1e
updated homeworks
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
264 
diff
changeset
 | 
158  | 
%                  (q1) edge [loop above] node[above] {$a$} ()
 | 
| 
 
a1544b804d1e
updated homeworks
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
264 
diff
changeset
 | 
159  | 
%                  (q2) edge [loop above] node[above] {$a, b$} ()
 | 
| 
 
a1544b804d1e
updated homeworks
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
264 
diff
changeset
 | 
160  | 
%                  (q1) edge node[above] {$b$} (q2)
 | 
| 
 
a1544b804d1e
updated homeworks
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
264 
diff
changeset
 | 
161  | 
%                  (q0) edge[bend right] node[below] {$a$} (q2)
 | 
| 
 
a1544b804d1e
updated homeworks
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
264 
diff
changeset
 | 
162  | 
% ;  | 
| 
 
a1544b804d1e
updated homeworks
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
264 
diff
changeset
 | 
163  | 
%\end{tikzpicture}
 | 
| 
 
a1544b804d1e
updated homeworks
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
264 
diff
changeset
 | 
164  | 
%\end{center}
 | 
| 
 
a1544b804d1e
updated homeworks
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
264 
diff
changeset
 | 
165  | 
%find the corresponding minimal automaton. State clearly which nodes  | 
| 
 
a1544b804d1e
updated homeworks
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
264 
diff
changeset
 | 
166  | 
%can be merged.  | 
| 31 | 167  | 
|
| 
355
 
a259eec25156
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
347 
diff
changeset
 | 
168  | 
\item Given the following non-deterministic finite automaton  | 
| 
 
a259eec25156
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
347 
diff
changeset
 | 
169  | 
      over the alphabet $\{a, b\}$, find a deterministic
 | 
| 
 
a259eec25156
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
347 
diff
changeset
 | 
170  | 
finite automaton that recognises the same language:  | 
| 
267
 
a1544b804d1e
updated homeworks
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
264 
diff
changeset
 | 
171  | 
|
| 
 
a1544b804d1e
updated homeworks
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
264 
diff
changeset
 | 
172  | 
  \begin{center}
 | 
| 
292
 
7ed2a25dd115
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
271 
diff
changeset
 | 
173  | 
    \begin{tikzpicture}[>=stealth',very thick,auto,
 | 
| 
 
7ed2a25dd115
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
271 
diff
changeset
 | 
174  | 
                        every state/.style={minimum size=0pt,
 | 
| 
 
7ed2a25dd115
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
271 
diff
changeset
 | 
175  | 
inner sep=2pt,draw=blue!50,very thick,  | 
| 
 
7ed2a25dd115
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
271 
diff
changeset
 | 
176  | 
fill=blue!20},scale=2]  | 
| 517 | 177  | 
      \node[state, initial]        (q0) at ( 0,1) {$Q_0$};
 | 
178  | 
      \node[state]                    (q1) at ( 1,1) {$Q_1$};
 | 
|
179  | 
      \node[state, accepting] (q2) at ( 2,1) {$Q_2$};
 | 
|
| 
267
 
a1544b804d1e
updated homeworks
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
264 
diff
changeset
 | 
180  | 
      \path[->] (q0) edge node[above] {$a$} (q1)
 | 
| 
 
a1544b804d1e
updated homeworks
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
264 
diff
changeset
 | 
181  | 
                (q0) edge [loop above] node[above] {$b$} ()
 | 
| 
 
a1544b804d1e
updated homeworks
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
264 
diff
changeset
 | 
182  | 
                (q0) edge [loop below] node[below] {$a$} ()
 | 
| 
 
a1544b804d1e
updated homeworks
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
264 
diff
changeset
 | 
183  | 
                (q1) edge node[above] {$a$} (q2);
 | 
| 
 
a1544b804d1e
updated homeworks
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
264 
diff
changeset
 | 
184  | 
    \end{tikzpicture}
 | 
| 
 
a1544b804d1e
updated homeworks
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
264 
diff
changeset
 | 
185  | 
  \end{center}
 | 
| 
 
a1544b804d1e
updated homeworks
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
264 
diff
changeset
 | 
186  | 
|
| 940 | 187  | 
   \solution{
 | 
188  | 
The DFA has three states Q0,Q1,Q2 with Q0 starting state and Q2 accepting.  | 
|
189  | 
The transitions are (Q0,a)-> Q1 (Q0,b)->Q0 (Q1,a)->Q2 (Q1,b)->Q0  | 
|
190  | 
(Q2,a)->Q2 (Q2,b)->Q0.  | 
|
191  | 
}  | 
|
192  | 
||
| 1000 | 193  | 
%\item %%\textbf{(Deleted for 2017, 2018, 2019)}
 | 
194  | 
% Given the following deterministic finite automaton over the  | 
|
195  | 
%  alphabet $\{0, 1\}$, find the corresponding minimal automaton. In
 | 
|
196  | 
% case states can be merged, state clearly which states can be merged.  | 
|
197  | 
%  | 
|
198  | 
%  \begin{center}
 | 
|
199  | 
%    \begin{tikzpicture}[>=stealth',very thick,auto,
 | 
|
200  | 
%                        every state/.style={minimum size=0pt,
 | 
|
201  | 
% inner sep=2pt,draw=blue!50,very thick,  | 
|
202  | 
% fill=blue!20},scale=2]  | 
|
203  | 
%      \node[state, initial]        (q0) at ( 0,1) {$Q_0$};
 | 
|
204  | 
%      \node[state]                    (q1) at ( 1,1) {$Q_1$};
 | 
|
205  | 
%      \node[state, accepting] (q4) at ( 2,1) {$Q_4$};
 | 
|
206  | 
%      \node[state]                    (q2) at (0.5,0) {$Q_2$};
 | 
|
207  | 
%      \node[state]                    (q3) at (1.5,0) {$Q_3$};
 | 
|
208  | 
%      \path[->] (q0) edge node[above] {$0$} (q1)
 | 
|
209  | 
%                (q0) edge node[right] {$1$} (q2)
 | 
|
210  | 
%                (q1) edge node[above] {$0$} (q4)
 | 
|
211  | 
%                (q1) edge node[right] {$1$} (q2)
 | 
|
212  | 
%                (q2) edge node[above] {$0$} (q3)
 | 
|
213  | 
%                (q2) edge [loop below] node {$1$} ()
 | 
|
214  | 
%                (q3) edge node[left] {$0$} (q4)
 | 
|
215  | 
%                (q3) edge [bend left=95, looseness = 2.2] node [left=2mm] {$1$} (q0)
 | 
|
216  | 
%                (q4) edge [loop right] node {$0, 1$} ();
 | 
|
217  | 
%    \end{tikzpicture}
 | 
|
218  | 
%  \end{center}
 | 
|
| 
271
 
b9b54574ee41
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
267 
diff
changeset
 | 
219  | 
|
| 1000 | 220  | 
%  \solution{Q0 and Q2 can be merged; and Q1 and Q3 as well}
 | 
| 892 | 221  | 
|
| 
267
 
a1544b804d1e
updated homeworks
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
264 
diff
changeset
 | 
222  | 
\item Given the following finite deterministic automaton over the alphabet $\{a, b\}$:
 | 
| 
264
 
4deef8ac5d72
uodated hws
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
258 
diff
changeset
 | 
223  | 
|
| 
267
 
a1544b804d1e
updated homeworks
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
264 
diff
changeset
 | 
224  | 
  \begin{center}
 | 
| 
292
 
7ed2a25dd115
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
271 
diff
changeset
 | 
225  | 
    \begin{tikzpicture}[scale=2,>=stealth',very thick,auto,
 | 
| 
 
7ed2a25dd115
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
271 
diff
changeset
 | 
226  | 
                        every state/.style={minimum size=0pt,
 | 
| 
 
7ed2a25dd115
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
271 
diff
changeset
 | 
227  | 
inner sep=2pt,draw=blue!50,very thick,  | 
| 
 
7ed2a25dd115
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
271 
diff
changeset
 | 
228  | 
fill=blue!20}]  | 
| 517 | 229  | 
      \node[state, initial, accepting]        (q0) at ( 0,1) {$Q_0$};
 | 
230  | 
      \node[state, accepting]                    (q1) at ( 1,1) {$Q_1$};
 | 
|
231  | 
      \node[state] (q2) at ( 2,1) {$Q_2$};
 | 
|
| 
267
 
a1544b804d1e
updated homeworks
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
264 
diff
changeset
 | 
232  | 
      \path[->] (q0) edge[bend left] node[above] {$a$} (q1)
 | 
| 
 
a1544b804d1e
updated homeworks
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
264 
diff
changeset
 | 
233  | 
                (q1) edge[bend left] node[above] {$b$} (q0)
 | 
| 
 
a1544b804d1e
updated homeworks
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
264 
diff
changeset
 | 
234  | 
                (q2) edge[bend left=50] node[below] {$b$} (q0)
 | 
| 
 
a1544b804d1e
updated homeworks
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
264 
diff
changeset
 | 
235  | 
                (q1) edge node[above] {$a$} (q2)
 | 
| 
 
a1544b804d1e
updated homeworks
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
264 
diff
changeset
 | 
236  | 
                (q2) edge [loop right] node {$a$} ()
 | 
| 
 
a1544b804d1e
updated homeworks
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
264 
diff
changeset
 | 
237  | 
                (q0) edge [loop below] node {$b$} ()
 | 
| 
 
a1544b804d1e
updated homeworks
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
264 
diff
changeset
 | 
238  | 
;  | 
| 
 
a1544b804d1e
updated homeworks
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
264 
diff
changeset
 | 
239  | 
    \end{tikzpicture}
 | 
| 
 
a1544b804d1e
updated homeworks
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
264 
diff
changeset
 | 
240  | 
  \end{center}
 | 
| 31 | 241  | 
|
| 
267
 
a1544b804d1e
updated homeworks
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
264 
diff
changeset
 | 
242  | 
Give a regular expression that can recognise the same language as  | 
| 
 
a1544b804d1e
updated homeworks
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
264 
diff
changeset
 | 
243  | 
this automaton. (Hint: If you use Brzozwski's method, you can assume  | 
| 
 
a1544b804d1e
updated homeworks
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
264 
diff
changeset
 | 
244  | 
Arden's lemma which states that an equation of the form $q = q\cdot r + s$  | 
| 
 
a1544b804d1e
updated homeworks
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
264 
diff
changeset
 | 
245  | 
has the unique solution $q = s \cdot r^*$.)  | 
| 
294
 
c29853b672fb
updated hws
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
292 
diff
changeset
 | 
246  | 
|
| 940 | 247  | 
  \solution{
 | 
248  | 
$(b + ab + aa(a^*)b)^* \cdot (1 + a)$  | 
|
249  | 
}  | 
|
250  | 
||
| 
294
 
c29853b672fb
updated hws
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
292 
diff
changeset
 | 
251  | 
\item If a non-deterministic finite automaton (NFA) has  | 
| 770 | 252  | 
$n$ states. How many states does a deterministic  | 
253  | 
automaton (DFA) that can recognise the same language  | 
|
254  | 
as the NFA maximal need?  | 
|
| 
294
 
c29853b672fb
updated hws
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
292 
diff
changeset
 | 
255  | 
|
| 936 | 256  | 
  \solution{$2^n$ in the worst-case and for some regexes the worst case
 | 
| 892 | 257  | 
cannot be avoided.  | 
258  | 
||
259  | 
    Other comments: $r^{\{n\}}$ can only be represented as $n$
 | 
|
260  | 
copies of the automaton for $r$, which can explode the automaton for bounded  | 
|
261  | 
regular expressions. Similarly, we have no idea how backreferences can be  | 
|
262  | 
represented as automaton.  | 
|
263  | 
}  | 
|
264  | 
||
| 936 | 265  | 
\item Rust implements a non-backtracking regular expression matcher  | 
266  | 
based on the classic idea of DFAs. Still, some regular expressions  | 
|
267  | 
take a surprising amount of time for matching problems. Explain the  | 
|
268  | 
problem?  | 
|
269  | 
||
270  | 
  \solution{The problem has to do with bounded regular expressions,
 | 
|
271  | 
    such as $r^{\{n\}}$. They are represented as $n$-copies of some
 | 
|
272  | 
automaton for $r$. If $n$ is large, then this can result in a  | 
|
273  | 
large memory-footprint and slow runtime.}  | 
|
274  | 
||
| 1000 | 275  | 
\item On Mentimeter there was a question: \textit{``Why does the [regex] $(a^*)^*b$ takes much longer for 
 | 
276  | 
strings of length 28 compared to say 25?''}\smallskip\\  | 
|
277  | 
||
| 1006 | 278  | 
For this consider a lake with $1000 m^2$ surface and an invasive plant  | 
| 1000 | 279  | 
that tries to cover the lake with leaves, think of the famous water lily that  | 
280  | 
produces leaves on which you can stand. This plant starts out with a  | 
|
| 1006 | 281  | 
seedling covering just $0.001 m^2$ of the lake, but doubles every day  | 
282  | 
the surface that is covers. So on day two it would cover $0.002 m^2$,  | 
|
283  | 
on day three $0.004 m^2$ and so on. How many days does the plant need to  | 
|
| 1000 | 284  | 
cover the entire lake? How many days is the lake still 90\% \emph{un}covered? 
 | 
285  | 
||
286  | 
\solution{That is a classic example of the law of exponentiation, meaning an 
 | 
|
287  | 
exponential function grows very slowly at first, but then explodes. It should take  | 
|
288  | 
20 days to completely cover the lake: $0.001 * 2^{20}$. But up to day 16 still less
 | 
|
289  | 
than 10\% are covered. The remaining 90\% covering comes essentially in the last 3  | 
|
290  | 
days only. That is the same with any exponential algorithm: they are pretty ok for some  | 
|
291  | 
small values, but then they suddenly explode where they are not ok anymore.\\  | 
|
292  | 
||
293  | 
PS: After COVID, we should all be more aware of the incredible growth of  | 
|
294  | 
exponential functions. That is why I liked that Ms~Merkel was in  | 
|
295  | 
charge of Germany during COVID and managed to keep numbers of dead  | 
|
296  | 
people in Germany relatively low...not all was smooth of course. But she  | 
|
297  | 
was a scientist in her former life (actually a physicist) and knew about  | 
|
298  | 
exponential growth. While we over here had this clown Boris Johnson in charge,  | 
|
299  | 
who with his joke-education and smashing up restaurants, had no clue what an  | 
|
300  | 
exponential function is.}  | 
|
301  | 
||
| 770 | 302  | 
\item Prove that for all regular expressions $r$ we have  | 
303  | 
||
304  | 
\begin{center} 
 | 
|
305  | 
  $\textit{nullable}(r) \quad \text{if and only if} 
 | 
|
306  | 
\quad [] \in L(r)$  | 
|
307  | 
\end{center}
 | 
|
308  | 
||
309  | 
Write down clearly in each case what you need to prove  | 
|
310  | 
and what are the assumptions.  | 
|
311  | 
||
312  | 
||
| 
444
 
3056a4c071b0
updated
 
Christian Urban <christian dot urban at kcl dot ac dot uk> 
parents: 
401 
diff
changeset
 | 
313  | 
\item \POSTSCRIPT  | 
| 23 | 314  | 
\end{enumerate}
 | 
315  | 
||
316  | 
\end{document}
 | 
|
317  | 
||
318  | 
%%% Local Variables:  | 
|
319  | 
%%% mode: latex  | 
|
320  | 
%%% TeX-master: t  | 
|
321  | 
%%% End:  |