hws/hw02.tex
author Christian Urban <christian.urban@kcl.ac.uk>
Mon, 10 Oct 2022 13:53:10 +0100
changeset 885 526aaee62a3e
parent 881 3b2f76950473
child 889 00c1c3408c93
permissions -rw-r--r--
updated
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
22
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     1
\documentclass{article}
267
a1544b804d1e updated homeworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 258
diff changeset
     2
\usepackage{../style}
22
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
     3
885
526aaee62a3e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 881
diff changeset
     4
\newcommand{\solution}[1]{%
526aaee62a3e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 881
diff changeset
     5
  \begin{quote}\sf%
526aaee62a3e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 881
diff changeset
     6
    #1%
526aaee62a3e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 881
diff changeset
     7
  \end{quote}}
526aaee62a3e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 881
diff changeset
     8
\renewcommand{\solution}[1]{}
526aaee62a3e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 881
diff changeset
     9
526aaee62a3e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 881
diff changeset
    10
22
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    11
\begin{document}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    12
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    13
\section*{Homework 2}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    14
347
22b5294daa2a updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 294
diff changeset
    15
\HEADER
22b5294daa2a updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 294
diff changeset
    16
22
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    17
\begin{enumerate}
499
dfd0f41f8668 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
    18
\item What is the difference between \emph{basic} regular expressions  
885
526aaee62a3e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 881
diff changeset
    19
  and \emph{extended} regular expressions?
526aaee62a3e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 881
diff changeset
    20
526aaee62a3e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 881
diff changeset
    21
  \solution{Basic regular expressions are $\ZERO$, $\ONE$, $c$, $r_1 + r_2$,
526aaee62a3e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 881
diff changeset
    22
    $r_1 \cdot r_2$, $r^*$. The extended ones are the bounded
526aaee62a3e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 881
diff changeset
    23
    repetitions, not, etc.}
499
dfd0f41f8668 updated
Christian Urban <urbanc@in.tum.de>
parents: 471
diff changeset
    24
  
401
5d85dc9779b1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
    25
\item What is the language recognised by the regular
885
526aaee62a3e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 881
diff changeset
    26
  expressions $(\ZERO^*)^*$.
526aaee62a3e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 881
diff changeset
    27
526aaee62a3e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 881
diff changeset
    28
  \solution{$L(\ZERO^*{}^*) = \{[]\}$,
526aaee62a3e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 881
diff changeset
    29
    remember * always includes the empty string}
258
1e4da6d2490c updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 132
diff changeset
    30
401
5d85dc9779b1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
    31
\item Review the first handout about sets of strings and read
5d85dc9779b1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
    32
      the second handout. Assuming the alphabet is the set
5d85dc9779b1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
    33
      $\{a, b\}$, decide which of the following equations are
5d85dc9779b1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
    34
      true in general for arbitrary languages $A$, $B$ and
5d85dc9779b1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
    35
      $C$:
115
86c1c049eb3e updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 104
diff changeset
    36
401
5d85dc9779b1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
    37
      \begin{eqnarray}
5d85dc9779b1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
    38
      (A \cup B) @ C & =^? & A @ C \cup B @ C\nonumber\\
5d85dc9779b1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
    39
      A^* \cup B^*   & =^? & (A \cup B)^*\nonumber\\
5d85dc9779b1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
    40
      A^* @ A^*      & =^? & A^*\nonumber\\
5d85dc9779b1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
    41
      (A \cap B)@ C  & =^? & (A@C) \cap (B@C)\nonumber
5d85dc9779b1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
    42
      \end{eqnarray}
104
ffde837b1db1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 102
diff changeset
    43
401
5d85dc9779b1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
    44
      \noindent In case an equation is true, give an
5d85dc9779b1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
    45
      explanation; otherwise give a counter-example.
104
ffde837b1db1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 102
diff changeset
    46
885
526aaee62a3e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 881
diff changeset
    47
      \solution{1 + 3 are equal; 2 + 4 are not. Interesting is 4 where
526aaee62a3e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 881
diff changeset
    48
      $A = \{[a]\}$, $B = \{[]\}$ and $C = \{[a], []\}$}
526aaee62a3e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 881
diff changeset
    49
401
5d85dc9779b1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
    50
\item Given the regular expressions $r_1 = \ONE$ and $r_2 =
5d85dc9779b1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
    51
      \ZERO$ and $r_3 = a$. How many strings can the regular
5d85dc9779b1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
    52
      expressions $r_1^*$, $r_2^*$ and $r_3^*$ each match?
104
ffde837b1db1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 102
diff changeset
    53
885
526aaee62a3e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 881
diff changeset
    54
      \solution{$r_1$ and $r_2$ can match the empty string only, $r_3$ can
526aaee62a3e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 881
diff changeset
    55
        match $[]$, $a$, $aa$, ....}
526aaee62a3e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 881
diff changeset
    56
401
5d85dc9779b1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
    57
\item Give regular expressions for (a) decimal numbers and for
617
f7de0915fff2 updated
Christian Urban <urbanc@in.tum.de>
parents: 499
diff changeset
    58
      (b) binary numbers. Hint: Observe that the empty string
401
5d85dc9779b1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
    59
      is not a number. Also observe that leading 0s are
617
f7de0915fff2 updated
Christian Urban <urbanc@in.tum.de>
parents: 499
diff changeset
    60
      normally not written---for example the JSON format for numbers
f7de0915fff2 updated
Christian Urban <urbanc@in.tum.de>
parents: 499
diff changeset
    61
      explicitly forbids this.
22
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    62
885
526aaee62a3e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 881
diff changeset
    63
      \solution{Just numbers without leading 0s: $0 + (1..9)\cdot(0..1)^*$;
526aaee62a3e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 881
diff changeset
    64
        can be extended to decimal; similar for binary numbers
526aaee62a3e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 881
diff changeset
    65
      }
526aaee62a3e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 881
diff changeset
    66
258
1e4da6d2490c updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 132
diff changeset
    67
\item Decide whether the following two regular expressions are
401
5d85dc9779b1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
    68
      equivalent $(\ONE + a)^* \equiv^? a^*$ and $(a \cdot
5d85dc9779b1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
    69
      b)^* \cdot a \equiv^? a \cdot (b \cdot a)^*$.
22
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    70
885
526aaee62a3e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 881
diff changeset
    71
      \solution{Both are equivalent, but why the second? Essentially you have to show that each string in one set is in the other. For 2 this means you can do an induction proof that $(ab)^na$ is the same string as $a(ba)^n$, where the former is in the first set and the latter in the second.}
526aaee62a3e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 881
diff changeset
    72
401
5d85dc9779b1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
    73
\item Given the regular expression $r = (a \cdot b + b)^*$.
5d85dc9779b1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
    74
      Compute what the derivative of $r$ is with respect to
5d85dc9779b1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
    75
      $a$, $b$ and $c$. Is $r$ nullable?
22
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
    76
881
3b2f76950473 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 880
diff changeset
    77
\item Give an argument for why the following holds:
885
526aaee62a3e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 881
diff changeset
    78
  if $r$ is nullable then $r^{\{n\}} \equiv r^{\{..n\}}$.
526aaee62a3e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 881
diff changeset
    79
526aaee62a3e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 881
diff changeset
    80
  \solution{This was from last week; I just explicitly added it here.}
258
1e4da6d2490c updated programs
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 132
diff changeset
    81
  
355
a259eec25156 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 347
diff changeset
    82
\item Define what is meant by the derivative of a regular
a259eec25156 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 347
diff changeset
    83
      expressions with respect to a character. (Hint: The
a259eec25156 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 347
diff changeset
    84
      derivative is defined recursively.)
267
a1544b804d1e updated homeworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 258
diff changeset
    85
885
526aaee62a3e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 881
diff changeset
    86
      \solution{the recursive function for $der$}
526aaee62a3e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 881
diff changeset
    87
      
768
34f77b976b88 updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 617
diff changeset
    88
\item  Assume the set $Der$ is defined as
267
a1544b804d1e updated homeworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 258
diff changeset
    89
a1544b804d1e updated homeworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 258
diff changeset
    90
  \begin{center}
a1544b804d1e updated homeworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 258
diff changeset
    91
    $Der\,c\,A \dn \{ s \;|\;  c\!::\!s \in A\}$
a1544b804d1e updated homeworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 258
diff changeset
    92
  \end{center}
a1544b804d1e updated homeworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 258
diff changeset
    93
401
5d85dc9779b1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
    94
      What is the relation between $Der$ and the notion of
5d85dc9779b1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
    95
      derivative of regular expressions?
267
a1544b804d1e updated homeworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 258
diff changeset
    96
885
526aaee62a3e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 881
diff changeset
    97
      \solution{Main property is $L(der\,c\,r) = Der\,c\,(L(r))$.}
526aaee62a3e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 881
diff changeset
    98
267
a1544b804d1e updated homeworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 258
diff changeset
    99
\item Give a regular expression over the alphabet $\{a,b\}$
401
5d85dc9779b1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   100
      recognising all strings that do not contain any
5d85dc9779b1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   101
      substring $bb$ and end in $a$.
267
a1544b804d1e updated homeworks
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 258
diff changeset
   102
885
526aaee62a3e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 881
diff changeset
   103
      
526aaee62a3e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 881
diff changeset
   104
401
5d85dc9779b1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   105
\item Do $(a + b)^* \cdot b^+$ and $(a^* \cdot b^+) +
885
526aaee62a3e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 881
diff changeset
   106
  (b^*\cdot b^+)$ define the same language?
526aaee62a3e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 881
diff changeset
   107
526aaee62a3e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 881
diff changeset
   108
   \solution{No, the first one can match for example abababababbbbb}
294
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   109
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   110
\item Define the function $zeroable$ by recursion over regular
401
5d85dc9779b1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   111
      expressions. This function should satisfy the property
294
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   112
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   113
  \[
401
5d85dc9779b1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   114
  zeroable(r) \;\;\text{if and only if}\;\;L(r) = \{\}\qquad(*)
294
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   115
  \]
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   116
401
5d85dc9779b1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   117
      The function $nullable$ for the not-regular expressions
5d85dc9779b1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   118
      can be defined by 
294
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   119
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   120
  \[
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   121
  nullable(\sim r) \dn \neg(nullable(r))
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   122
  \]
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   123
401
5d85dc9779b1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   124
      Unfortunately, a similar definition for $zeroable$ does
5d85dc9779b1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   125
      not satisfy the property in $(*)$:
294
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   126
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   127
  \[
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   128
  zeroable(\sim r) \dn \neg(zeroable(r))
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   129
  \]
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   130
471
9476086849ad updated
Christian Urban <urbanc@in.tum.de>
parents: 444
diff changeset
   131
      Find a counter example?
294
c29853b672fb updated hws
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 292
diff changeset
   132
401
5d85dc9779b1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   133
\item Give a regular expressions that can recognise all
5d85dc9779b1 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 355
diff changeset
   134
      strings from the language $\{a^n\;|\;\exists k.\; n = 3 k
885
526aaee62a3e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 881
diff changeset
   135
      + 1 \}$.
526aaee62a3e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 881
diff changeset
   136
526aaee62a3e updated
Christian Urban <christian.urban@kcl.ac.uk>
parents: 881
diff changeset
   137
      \solution{$a(aaa)^*$}
404
245d302791c7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 401
diff changeset
   138
      
245d302791c7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 401
diff changeset
   139
\item Give a regular expression that can recognise an odd 
245d302791c7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 401
diff changeset
   140
number of $a$s or an even number of $b$s.     
444
3056a4c071b0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 404
diff changeset
   141
3056a4c071b0 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 404
diff changeset
   142
\item \POSTSCRIPT  
404
245d302791c7 updated
Christian Urban <christian dot urban at kcl dot ac dot uk>
parents: 401
diff changeset
   143
\end{enumerate}
22
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   144
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   145
\end{document}
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   146
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   147
%%% Local Variables: 
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   148
%%% mode: latex
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   149
%%% TeX-master: t
Christian Urban <urbanc@in.tum.de>
parents:
diff changeset
   150
%%% End: