KMP.
1 \documentclass[a4paper, 10pt, pagesize, smallheadings]{article}
3 %\usepackage[latin1]{inputenc}
4 \usepackage{amsmath, amsthm, amssymb}
7 \usepackage{algorithmic}
10 \title{Theory I, Sheet 4 Solution}
12 \renewcommand{\familydefault}{\sfdefault}
13 \include{pythonlisting}
20 \section*{Exercise 4.1}
25 & & *++[o][F-]{50} \ar@{-}[dl] \ar@{-}[dr] & & & & & & *++[o][F-]{50} \ar@{-}[dl] \ar@{-}[dr]\\
26 & *++[o][F-]{30} \ar@{-}[dl] \ar@{-}[dr] & & *++[o][F-]{60} \ar@{-}[dr] & \ar@{=>}[rr]^{delete(40)}_{upout(30)} & & & *++[o][F-]{30} \ar@{-}[dl] & & *++[o][F-]{60}\ar@{-}[dr]\\
27 *++[o][F-]{20} & & *++[o][F-]{40} & & *++[o][F-]{70} & & *++[o][F-]{20} & & & & *++[o][F-]{70}\\
33 & & *++[o][F-]{50} \ar@{-}[dl] \ar@{-}[dr] & & & & & & *++[o][F-]{50} \ar@{-}[dl] \ar@{-}[dr]\\
34 & *++[o][F-]{30} \ar@{-}[dl] & & *++[o][F-]{60} \ar@{-}[dr] & \ar@{=>}[rr]^{delete(60)}_{upout(50)} & & & *++[o][F-]{30} \ar@{-}[dl] & & *++[o][F-]{70}\\
35 *++[o][F-]{20} & & & & *++[o][F-]{70} & & *++[o][F-]{20} & & & & \\
38 There are no rotations required because the AVL-balance property is not violated.
40 \section*{Exercise 4.2}
41 We use the following Python function for the calculation of the hash code, which resembles the Java code presented in the lecture:
44 return sum(map(ord, string)) % m
46 We choose $m = 13$ and get following results:\\\\
47 \begin{tabular}{ l c l}
48 \textbf{string} & \textbf{hash} & \textbf{collisions}\\ \hline
49 \emph{Hashing} & 4 & \\
50 \emph{Exercise} & 5 & \\
51 \emph{THEORYI} & 2 & \\
52 \emph{god} & 2 & \emph{THEORYI}\\
53 \emph{science} & 2 & \emph{THEORYI, god}\\
55 As you can see there are conflicts between \emph{god, science} and \emph{THEORYI}.
57 \section*{Exercise 4.3}
58 Let $U = \{0,...,N-1\}$, where $N = 49$ and $m = 35$. Let $a_i = 42i$ and $b_i = 28i$.\\
59 Let $H = \{h_i(k) = ((a_ik + b_i) \bmod N) \bmod m\}$ for $ i \in \{1,...,N(N-1)\}$ be a class of hash functions. We show that $H$ is \emph{not} universal by contradiction.\\\\
60 We assume $H$ is universal.
61 % \left\{ x \in X \mid x > \frac{1}{2}\right\} \]
62 \[\iff \frac{|\{h \in H \mid h(x) = h(y) \land x \neq y\}|}{|H|} \leq \frac{1}{m}\]
63 First we look at the mapping space of the inner modulo function for all pairs $(q, r)$ with \\$q = (a_ix + b_i) \bmod N$ and $r = (a_iy + b_i) \bmod N$ for all $x,y \in U$ and $x \neq y$.
64 \[q = r \iff (a_ix + b_i) \bmod N = (a_iy + b_i) \bmod N\]
65 \[\iff a_i(x - y) \bmod N = 0\]
66 \[\iff \exists_{c \in \mathbb{N}}: a_i(x - y) = cN\]
67 \[\implies a_i|N \land (x - y)|N\]
68 \[\iff \exists_{l \in \mathbb{N}, 0<l\leq7}: (x - y) = 7l\]
69 It follows that there are collisions for $(x - y) = 7l$ with $c = \frac{a_i \cdot 7l}{N} = \frac{42i \cdot 7l}{49} = 6il$\\
70 To determine the number of inner collisions, we calculate $\sum_{0 < l \leq 7} (48 - 7l)$.
71 \[\sum_{0 < l \leq 7} (48 - 7l) = 7(48 - \sum_{0 < l \leq 7}l) = 140\]
72 We know that the number of total collisions of a hash function is greater or equal to the number of collisions of its inner modulo mapping space.
73 \[ \implies \frac{140}{|H|} = \frac{140}{49 \cdot 48} > \frac{1}{35} \implies Contradiction!\]
74 Even within the mapping space of the inner modulo function we get more collisions than accepted for a universal class of hash functions; it follows that $H$ is \emph{not} universal. \qed