hws/hw01.tex
author Christian Urban <christian.urban@kcl.ac.uk>
Thu, 05 Oct 2023 14:36:54 +0100
changeset 940 46eee459a999
parent 935 4e221cf587fa
child 962 5176cbb819c2
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
631
f618dd4de24a updated
Christian Urban <urbanc@in.tum.de>
parents: 550
diff changeset
     1
% !TEX program = xelatex
0
3a5e09a2ae54 initial comit
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     2
\documentclass{article}
249
377c59df7297 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 227
diff changeset
     3
\usepackage{../style}
0
3a5e09a2ae54 initial comit
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     4
3a5e09a2ae54 initial comit
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     5
\begin{document}
3a5e09a2ae54 initial comit
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     6
3a5e09a2ae54 initial comit
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     7
\section*{Homework 1}
3a5e09a2ae54 initial comit
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     8
331
a2c18456c6b7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 294
diff changeset
     9
\HEADER
a2c18456c6b7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 294
diff changeset
    10
0
3a5e09a2ae54 initial comit
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
\begin{enumerate}
249
377c59df7297 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 227
diff changeset
    12
401
5d85dc9779b1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 394
diff changeset
    13
\item {\bf (Optional)} If you want to run the code presented
5d85dc9779b1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 394
diff changeset
    14
      in the lectures, install the Scala programming language
5d85dc9779b1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 394
diff changeset
    15
      available (for free) from
249
377c59df7297 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 227
diff changeset
    16
743
6acabeecdf75 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 726
diff changeset
    17
      \begin{center}
6acabeecdf75 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 726
diff changeset
    18
        \url{http://www.scala-lang.org}
6acabeecdf75 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 726
diff changeset
    19
      \end{center}
6acabeecdf75 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 726
diff changeset
    20
6acabeecdf75 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 726
diff changeset
    21
       and the Ammonite REPL from
6acabeecdf75 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 726
diff changeset
    22
6acabeecdf75 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 726
diff changeset
    23
       \begin{center}
6acabeecdf75 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 726
diff changeset
    24
       \url{https://ammonite.io}
6acabeecdf75 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 726
diff changeset
    25
       \end{center}      
0
3a5e09a2ae54 initial comit
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    26
401
5d85dc9779b1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 394
diff changeset
    27
      If you want to follow the code I present during the
927
ef54868a9226 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 922
diff changeset
    28
      lectures, read the handout about Scala. Make sure Ammonite
ef54868a9226 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 922
diff changeset
    29
      uses the Scala 3 compiler.
0
3a5e09a2ae54 initial comit
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    30
639
217e66d7aeff updated
Christian Urban <urbanc@in.tum.de>
parents: 631
diff changeset
    31
%\item {\bf (Optional)} Have a look at the crawler programs.
217e66d7aeff updated
Christian Urban <urbanc@in.tum.de>
parents: 631
diff changeset
    32
%      Can you find a usage for them in your daily programming
217e66d7aeff updated
Christian Urban <urbanc@in.tum.de>
parents: 631
diff changeset
    33
%      life? Can you improve them? For example in cases there
217e66d7aeff updated
Christian Urban <urbanc@in.tum.de>
parents: 631
diff changeset
    34
%      are links that appear on different recursion levels, the
217e66d7aeff updated
Christian Urban <urbanc@in.tum.de>
parents: 631
diff changeset
    35
%      crawlers visit such web-pages several times. Can this be
217e66d7aeff updated
Christian Urban <urbanc@in.tum.de>
parents: 631
diff changeset
    36
%      avoided? Also, the crawlers flag as problematic any page
217e66d7aeff updated
Christian Urban <urbanc@in.tum.de>
parents: 631
diff changeset
    37
%      that gives an error, but probably only 404 Not Found
217e66d7aeff updated
Christian Urban <urbanc@in.tum.de>
parents: 631
diff changeset
    38
%      errors should be flagged. Can you change that?)
104
ffde837b1db1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 102
diff changeset
    39
640
281139526cb1 updated
Christian Urban <urbanc@in.tum.de>
parents: 639
diff changeset
    40
\item {\bf (Optional)} Have a look at the catastrophic backtracking
281139526cb1 updated
Christian Urban <urbanc@in.tum.de>
parents: 639
diff changeset
    41
  programs uploaded on KEATS. Convince yourself that they really require
281139526cb1 updated
Christian Urban <urbanc@in.tum.de>
parents: 639
diff changeset
    42
  a lot of computation time. If you have similar examples in your own
281139526cb1 updated
Christian Urban <urbanc@in.tum.de>
parents: 639
diff changeset
    43
  favourite programming language, I am happy to hear about it.
281139526cb1 updated
Christian Urban <urbanc@in.tum.de>
parents: 639
diff changeset
    44
281139526cb1 updated
Christian Urban <urbanc@in.tum.de>
parents: 639
diff changeset
    45
401
5d85dc9779b1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 394
diff changeset
    46
\item Read the handout of the first lecture and the handout
5d85dc9779b1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 394
diff changeset
    47
      about notation. Make sure you understand the concepts of
498
ea47c3b8f35f updated
Christian Urban <urbanc@in.tum.de>
parents: 444
diff changeset
    48
      strings and languages. In the context of the CFL-course,
401
5d85dc9779b1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 394
diff changeset
    49
      what is meant by the term \emph{language}?
9
Christian Urban <urbanc@in.tum.de>
parents: 0
diff changeset
    50
876
771396fa6cc4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 841
diff changeset
    51
      \solution{A language - in this context - is just a set of
771396fa6cc4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 841
diff changeset
    52
        strings. Some of these sets can actually not be described by
771396fa6cc4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 841
diff changeset
    53
        regular expressions. Only regular​ languages can. This is
771396fa6cc4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 841
diff changeset
    54
        something for lecture 3.}
771396fa6cc4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 841
diff changeset
    55
      
550
71fc4a7a7039 updated
Christian Urban <urbanc@in.tum.de>
parents: 507
diff changeset
    56
\item Give the definition for regular expressions---this is an
498
ea47c3b8f35f updated
Christian Urban <urbanc@in.tum.de>
parents: 444
diff changeset
    57
      inductive datatype. What is the
355
a259eec25156 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 331
diff changeset
    58
      meaning of a regular expression? (Hint: The meaning is
a259eec25156 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 331
diff changeset
    59
      defined recursively.)
0
3a5e09a2ae54 initial comit
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    60
876
771396fa6cc4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 841
diff changeset
    61
      \solution{Here I would also expect the grammar for basic regular
771396fa6cc4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 841
diff changeset
    62
        expressions and the definition of the recursive L-function. Discuss
771396fa6cc4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 841
diff changeset
    63
        differences between $r_1 + r_2$ and $r^+$. Discuss differences between
771396fa6cc4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 841
diff changeset
    64
      ``real-life regexes'' and regexes in this module.}
771396fa6cc4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 841
diff changeset
    65
355
a259eec25156 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 331
diff changeset
    66
\item Assume the concatenation operation of two strings is
a259eec25156 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 331
diff changeset
    67
      written as $s_1 @ s_2$. Define the operation of
a259eec25156 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 331
diff changeset
    68
      \emph{concatenating} two sets of strings. This operation
394
2f9fe225ecc8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
    69
      is also written as $\_ \,@\, \_$. According to 
2f9fe225ecc8 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
    70
      this definition, what is $A \,@\, \{\}$ equal to?
498
ea47c3b8f35f updated
Christian Urban <urbanc@in.tum.de>
parents: 444
diff changeset
    71
      Is in general $A\,@\,B$ equal to $B\,@\,A$?
0
3a5e09a2ae54 initial comit
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    72
876
771396fa6cc4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 841
diff changeset
    73
      \solution{ What is $A @ {[]}$? Are there special cases
771396fa6cc4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 841
diff changeset
    74
      where $A @ B = B @ A$? }
771396fa6cc4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 841
diff changeset
    75
355
a259eec25156 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 331
diff changeset
    76
\item Assume a set $A$ contains 4 strings and a set $B$
a259eec25156 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 331
diff changeset
    77
      contains 7 strings. None of the strings is the empty
a259eec25156 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 331
diff changeset
    78
      string. How many strings are in $A \,@\, B$?
249
377c59df7297 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 227
diff changeset
    79
876
771396fa6cc4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 841
diff changeset
    80
      \solution{28, but there are corner cases where there are fewer
771396fa6cc4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 841
diff changeset
    81
        than 28 elements. Can students think of such corner cases?
771396fa6cc4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 841
diff changeset
    82
      For example $A = \{a, ab, \ldots\}$, $B = \{bc, c,\ldots\}$ }
771396fa6cc4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 841
diff changeset
    83
267
a1544b804d1e updated homeworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 258
diff changeset
    84
\item How is the power of a language defined? (Hint: There are two
a1544b804d1e updated homeworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 258
diff changeset
    85
  rules, one for $\_^0$ and one for $\_^{n+1}$.)
109
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 104
diff changeset
    86
876
771396fa6cc4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 841
diff changeset
    87
     \solution{Two rules: 0-case and n+1 case.}
771396fa6cc4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 841
diff changeset
    88
355
a259eec25156 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 331
diff changeset
    89
\item Let $A = \{[a], [b], [c], [d]\}$. (1) How many strings
438
84608b4b3578 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 416
diff changeset
    90
      are in $A^4$? (2) Consider also the case of $A^4$ where one of
355
a259eec25156 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 331
diff changeset
    91
      the strings in $A$ is the empty string, for example $A =
a259eec25156 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 331
diff changeset
    92
      \{[a], [b], [c], []\}$.
293
ca349cfe3474 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 267
diff changeset
    93
876
771396fa6cc4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 841
diff changeset
    94
      \solution{121 is correct. But make sure you understand why it is 121
771396fa6cc4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 841
diff changeset
    95
        in cases you do not have a computer at your fingertips.}
771396fa6cc4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 841
diff changeset
    96
507
fdbc7d0ec04f updated
Christian Urban <urbanc@in.tum.de>
parents: 498
diff changeset
    97
\item (1) How many basic regular expressions are there to match
776
939c10745a3a updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 743
diff changeset
    98
      \textbf{only} the string $abcd$? (2) How many if they cannot include
444
3056a4c071b0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 438
diff changeset
    99
      $\ONE$ and $\ZERO$? (3) How many if they are also not
3056a4c071b0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 438
diff changeset
   100
      allowed to contain stars? (4) How many if they are also
401
5d85dc9779b1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 394
diff changeset
   101
      not allowed to contain $\_ + \_$?
0
3a5e09a2ae54 initial comit
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   102
876
771396fa6cc4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 841
diff changeset
   103
      \solution{1-3 are infinite (tell the idea why - examples); 4 is five - remember regexes are trees.}
771396fa6cc4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 841
diff changeset
   104
      
355
a259eec25156 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 331
diff changeset
   105
\item When are two regular expressions equivalent? Can you
a259eec25156 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 331
diff changeset
   106
      think of instances where two regular expressions match
a259eec25156 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 331
diff changeset
   107
      the same strings, but it is not so obvious that they do?
a259eec25156 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 331
diff changeset
   108
      For example $a + b$ and $b + a$ do not count\ldots they
a259eec25156 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 331
diff changeset
   109
      obviously match the same strings, namely $[a]$ and
a259eec25156 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 331
diff changeset
   110
      $[b]$.
403
564f7584eff1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 401
diff changeset
   111
876
771396fa6cc4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 841
diff changeset
   112
      \solution{for example $r^* = 1 + r \cdot r^*$ for any regular expression $r$.
771396fa6cc4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 841
diff changeset
   113
        Can students think about why this is the case?}
771396fa6cc4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 841
diff changeset
   114
416
357c395ae838 updated
Christian Urban <urbanc@in.tum.de>
parents: 403
diff changeset
   115
\item What is meant by the notions \emph{evil regular expressions}
726
fba480bbc9f7 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 640
diff changeset
   116
  and by \emph{catastrophic backtracking}?
fba480bbc9f7 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 640
diff changeset
   117
876
771396fa6cc4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 841
diff changeset
   118
  \solution{catastrophic backtracking also applies to other regexes, not just $(a^*)^*b$}
771396fa6cc4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 841
diff changeset
   119
726
fba480bbc9f7 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 640
diff changeset
   120
\item Given the regular expression $(a + b)^* \cdot b \cdot (a + b)^*$,
841
564840440523 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 776
diff changeset
   121
  which of the following regular expressions are equivalent
726
fba480bbc9f7 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 640
diff changeset
   122
fba480bbc9f7 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 640
diff changeset
   123
\begin{center}
fba480bbc9f7 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 640
diff changeset
   124
\begin{tabular}{ll}    
fba480bbc9f7 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 640
diff changeset
   125
  1) & $(ab + bb)^* \cdot (a + b)^*$\\                     % no
fba480bbc9f7 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 640
diff changeset
   126
  2) & $(a + b)^* \cdot (ba + bb + b) \cdot (a + b)^*$\\   % yes
fba480bbc9f7 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 640
diff changeset
   127
  3) & $(a + b)^* \cdot (a + b) \cdot (a + b)^*$           % no
fba480bbc9f7 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 640
diff changeset
   128
\end{tabular}
fba480bbc9f7 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 640
diff changeset
   129
\end{center}
876
771396fa6cc4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 841
diff changeset
   130
771396fa6cc4 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 841
diff changeset
   131
  \solution{no, yes (why?), no.}
922
e86ea06e3b25 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   132
e86ea06e3b25 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   133
e86ea06e3b25 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   134
\item Given the extended regular expression \texttt{[b-d]a?e+},
e86ea06e3b25 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   135
  what does the equivalent basic regular expression look like?
726
fba480bbc9f7 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 640
diff changeset
   136
  
935
4e221cf587fa updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 927
diff changeset
   137
  \solution{$(b + c + d) \cdot (a + \ONE) \cdot (e \cdot e^*)$}
922
e86ea06e3b25 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   138
  
e86ea06e3b25 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 916
diff changeset
   139
  
403
564f7584eff1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 401
diff changeset
   140
\item \POSTSCRIPT  
0
3a5e09a2ae54 initial comit
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   141
\end{enumerate}
3a5e09a2ae54 initial comit
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   142
3a5e09a2ae54 initial comit
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   143
\end{document}
3a5e09a2ae54 initial comit
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   144
3a5e09a2ae54 initial comit
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   145
%%% Local Variables: 
3a5e09a2ae54 initial comit
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   146
%%% mode: latex
3a5e09a2ae54 initial comit
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   147
%%% TeX-master: t
3a5e09a2ae54 initial comit
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   148
%%% End: