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-- |
22 | 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 | 3 |
|
885 | 4 |
\newcommand{\solution}[1]{% |
5 |
\begin{quote}\sf% |
|
6 |
#1% |
|
7 |
\end{quote}} |
|
8 |
\renewcommand{\solution}[1]{} |
|
9 |
||
10 |
||
22 | 11 |
\begin{document} |
12 |
||
13 |
\section*{Homework 2} |
|
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 | 17 |
\begin{enumerate} |
499 | 18 |
\item What is the difference between \emph{basic} regular expressions |
885 | 19 |
and \emph{extended} regular expressions? |
20 |
||
21 |
\solution{Basic regular expressions are $\ZERO$, $\ONE$, $c$, $r_1 + r_2$, |
|
22 |
$r_1 \cdot r_2$, $r^*$. The extended ones are the bounded |
|
23 |
repetitions, not, etc.} |
|
499 | 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 | 26 |
expressions $(\ZERO^*)^*$. |
27 |
||
28 |
\solution{$L(\ZERO^*{}^*) = \{[]\}$, |
|
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 | 47 |
\solution{1 + 3 are equal; 2 + 4 are not. Interesting is 4 where |
48 |
$A = \{[a]\}$, $B = \{[]\}$ and $C = \{[a], []\}$} |
|
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 | 54 |
\solution{$r_1$ and $r_2$ can match the empty string only, $r_3$ can |
55 |
match $[]$, $a$, $aa$, ....} |
|
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 | 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 | 60 |
normally not written---for example the JSON format for numbers |
61 |
explicitly forbids this. |
|
22 | 62 |
|
885 | 63 |
\solution{Just numbers without leading 0s: $0 + (1..9)\cdot(0..1)^*$; |
64 |
can be extended to decimal; similar for binary numbers |
|
65 |
} |
|
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 | 70 |
|
885 | 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.} |
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 | 76 |
|
881 | 77 |
\item Give an argument for why the following holds: |
885 | 78 |
if $r$ is nullable then $r^{\{n\}} \equiv r^{\{..n\}}$. |
79 |
||
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 | 86 |
\solution{the recursive function for $der$} |
87 |
||
768 | 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 | 97 |
\solution{Main property is $L(der\,c\,r) = Der\,c\,(L(r))$.} |
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 | 103 |
|
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 | 106 |
(b^*\cdot b^+)$ define the same language? |
107 |
||
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 | 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 | 135 |
+ 1 \}$. |
136 |
||
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 | 144 |
|
145 |
\end{document} |
|
146 |
||
147 |
%%% Local Variables: |
|
148 |
%%% mode: latex |
|
149 |
%%% TeX-master: t |
|
150 |
%%% End: |