\documentclass[11pt]{article}
\usepackage{amsmath,amssymb,amsthm}
\usepackage[usenames,dvipsnames]{pstricks}
\usepackage{epsfig}
\DeclareMathOperator*{\E}{\mathbb{E}}
\let\Pr\relax
\DeclareMathOperator*{\Pr}{\mathbb{P}}

\newcommand{\eps}{\epsilon}
\newcommand{\inprod}[1]{\left\langle #1 \right\rangle}
\newcommand{\R}{\mathbb{R}}

\newcommand{\handout}[5]{
  \noindent
  \begin{center}
  \framebox{
    \vbox{
      \hbox to 5.78in { {\bf CS 388R: Randomized Algorithms } \hfill #2 }
      \vspace{4mm}
      \hbox to 5.78in { {\Large \hfill #5  \hfill} }
      \vspace{2mm}
      \hbox to 5.78in { {\em #3 \hfill #4} }
    }
  }
  \end{center}
  \vspace*{4mm}
}

\newcommand{\lecture}[4]{\handout{#1}{#2}{#3}{Scribe: #4}{Lecture #1}}

\newtheorem{theorem}{Theorem}
\newtheorem{corollary}[theorem]{Corollary}
\newtheorem{lemma}[theorem]{Lemma}
\newtheorem{observation}[theorem]{Observation}
\newtheorem{proposition}[theorem]{Proposition}
\newtheorem{definition}[theorem]{Definition}
\newtheorem{claim}[theorem]{Claim}
\newtheorem{fact}[theorem]{Fact}
\newtheorem{assumption}[theorem]{Assumption}

% 1-inch margins, from fullpage.sty by H.Partl, Version 2, Dec. 15, 1988.
\topmargin 0pt
\advance \topmargin by -\headheight
\advance \topmargin by -\headsep
\textheight 8.9in
\oddsidemargin 0pt
\evensidemargin \oddsidemargin
\marginparwidth 0.5in
\textwidth 6.5in

\parindent 0in
\parskip 1.5ex

\begin{document}

\lecture{10 - Oct. 5, 2015}{Fall 2015}{Prof.\ Eric Price}{Vivek Pradhan, Fu Li}

\section{Recap Cuckoo Hashing}

In the last lecture, we covered Cuckoo Hashing, which has many good properties: 
expected $O(1)$ time for insertion, worst case $O(1)$ time for lookup and delete. 
However, to construct the such good hash function, we ``cheated'' ourselves for assuming that all involved hash functions $h$ were fully independent, which is hard to be satisfied in the real world.

To avoid the fully independence assumption, we show, instead of fully independence, $O(\log n)$-wise independence suffices. Recall that, in the fully independent case, the main point is to show no component has size lager than $t$ with high probability. For  $O(\log n)$-wise independent case,  it is suffice for us to show the same statement still be true. That is, we need to show there are no circles in the Cuckoo graph with probability $(1-1/n)$ when we use $O(\log n)$-wise independent hashing function, which is easy to figure out. 

For the $O(\log n)$-independent hashing, there is a easy way to construct it by using $O(\log n)$-degree polynomial. However, it doesn't work.  To evaluate this hash function, it costs us $O(\log n)$ time, which breaks the promise of expected constant time lookup for Cuckoo Hashing. 

Besides the $O(\log n)$-independent hashing, we can also try to use other simpler hash functions. However, it is not known whether there are simple hash functions which could work for Cuckoo Hashing. Until  now, people only know there exists a $5$-wise independent hashing which cannot work.


\section{Perfect Hashing}

Note that, the Cuckoo Hashing still use $O(n)$ spaces to store the hash value. In the following, we want to minimize the size of the hash range. In this case, we focus on how to construct a hash function with constant-time lookup and the perfect (minimal) range, without concerning the insertion or deletion. Formally, we introduce the following definition:

\begin{definition}[Perfect Hashing]
Suppose we have a large universe $U$ of keys. For a $n$-sized set $S\subset U$, a perfect hash function $F:S\rightarrow [m]$ is a hash function that maps distinct elements in $S$ to a set of integers, with no collisions. 
\end{definition}

\begin{definition}[Minimal Perfect Hashing Function (MPHF)]
For a $n$-sized set $S$, a minimal perfect hashing function is a perfect hash function $F:S\rightarrow [m]$ where $m=n$.
\end{definition}

\textbf{Main Goal}:  $O(n)$ time and $O(n)$ space to construct the MPHF and can be evaluated in $O(1)$ time. 

In the following, we first try to show how to construct the PHF in $O(n)$ time and $O(n)$ space with $O(1)$ lookup time. 

Possible ways:
\begin{enumerate}
\item Use the identity function, namely $h(x)=x$, then it will use $m=U$ size. 
\item List, which is very slow for lookup
\item Use $2$-wise independent hashing over $2n^2$. 
\end{enumerate} 
To analyze the last method, there are ${n\choose 2}$ pairs and each
pair collides with probability $1/m$. Thus, by union bound, we know
$Pr_{h\sim H}[\text{$\exists$ collisions}]\leq {n\choose 2}/m\leq
1/4$. That is, $Pr_{h\sim H}[\text{no collisions}]\geq 3/4$.

Thus we can construct the hashing with an expected constant number of
hash functions.  We can check each hash function in linear time, for
expected linear construction time total.

But how can we decrease from $O(n^2)$ to $O(n)$ space?

\subsection{Constructing Perfect Hash}
\psscalebox{1.0 1.0} % Change this value to rescale the drawing.
{
\begin{pspicture}(0,-3.2)(9.2,3.2)
\rput{-90.0}(4.22,8.18){\pstriangle[linecolor=black, linewidth=0.04, dimen=outer](6.2,1.78)(0.4,0.4)}
\psframe[linecolor=black, linewidth=0.04, dimen=outer](4.4,2.4)(3.2,-3.2)
\psframe[linecolor=black, linewidth=0.04, dimen=outer](4.4,2.4)(3.2,1.6)
\psframe[linecolor=black, linewidth=0.04, dimen=outer](4.4,1.6)(3.2,0.8)
\psframe[linecolor=black, linewidth=0.04, dimen=outer](4.4,0.8)(3.2,0.0)
\psframe[linecolor=black, linewidth=0.04, dimen=outer](4.4,0.0)(3.2,-0.8)
\psframe[linecolor=black, linewidth=0.04, dimen=outer](4.4,-2.4)(3.2,-3.2)
\rput(2.8,1.2){$h^*(z)$}
\rput(4.8,2.0){$x_1$}
\rput(4.8,1.2){$x_2$}
\rput(4.8,0.4){$x_3$}
\rput(4.8,-2.8){$x_n$}
\psline[linecolor=black, linewidth=0.04](5.2,2.0)(6.0,2.0)(6.0,2.0)



\psframe[linecolor=black, linewidth=0.04, dimen=outer](8.4,2.4)(6.8,1.6)
\psframe[linecolor=black, linewidth=0.04, dimen=outer](7.6,2.4)(7.2,1.6)
\psframe[linecolor=black, linewidth=0.04, dimen=outer](8.4,2.4)(8.0,1.6)
\psframe[linecolor=black, linewidth=0.04, dimen=outer](7.2,2.4)(6.8,1.6)
\psframe[linecolor=black, linewidth=0.04, dimen=outer](8.0,2.4)(7.6,1.6)
\rput{-90.0}(5.02,7.38){\pstriangle[linecolor=black, linewidth=0.04, dimen=outer](6.2,0.98)(0.4,0.4)}
\psline[linecolor=black, linewidth=0.04](5.2,1.2)(6.0,1.2)(6.0,1.2)
\psframe[linecolor=black, linewidth=0.04, dimen=outer](9.2,1.6)(6.8,0.8)
\psframe[linecolor=black, linewidth=0.04, dimen=outer](7.2,1.6)(6.8,0.8)
\psframe[linecolor=black, linewidth=0.04, dimen=outer](7.6,1.6)(7.2,0.8)
\psframe[linecolor=black, linewidth=0.04, dimen=outer](8.0,1.6)(7.6,0.8)
\psframe[linecolor=black, linewidth=0.04, dimen=outer](8.02,1.6)(8.0,1.58)
\psframe[linecolor=black, linewidth=0.04, dimen=outer](8.4,1.6)(8.0,0.8)
\psframe[linecolor=black, linewidth=0.04, dimen=outer](8.8,1.6)(8.4,0.8)
\psframe[linecolor=black, linewidth=0.04, dimen=outer](9.2,1.6)(8.8,0.8)
\end{pspicture}
}


We first use a 2-wise independent hash function and hash into $n$ buckets. Then we take all the collisions and hash it into set of buckets of size $2x_i^2$ where $x_i$ is number of collisions in that bucket. Note that each row will have length $2 x_i^2$ so as not to have any collisions as shown in the figure.\\
Let $ Y_i = \sum_{j=1}^{i-1} 2 x_j^2 \Rightarrow$ our hash function is evaluated as follows (this just converts the outputs from n different hash function into sequential values):\\
$$h(z) = Y_h^*(z) + h_{h^*(z)}(z)$$
Let use analyze the space used to store the hash functions $h_i$. Note that each $h_i$ uses $2 x_i^2$ buckets. This means that space used to store the $n$ hash functions $h_i$ is $\text{space}(h_i) = \sum_i^n 2 x_i^2$\\
\[\sum_i^n 2 x_i^2 = 2 \sum_i^n {x_i \choose 2} + \sum_i^n x_i = 2 (\text{No. of Collisions}) + n\]
\[E\left[ \sum_i^n 2 x_i^2 \right] = 2 \left( \binom{n}{2}
  \dfrac{1}{n} \right) + n \leq \dfrac{n^2}{n} + n = 2n.\] Hence the
total number of buckets is at most $4n$ with $1/2$ probability.  Hence
overall space usage is $O(n)$ for storing the mapping $Y_i$, the $n$
hash functions $h_i$, $h^*$, and the contents of the buckets.

To construct the hash function, we first choose $h^*$ repeatedly until
we find one with $\sum 2 x_i^2 \leq 4n$.  We then choose the
individual $h_i$ until they have zero collisions.  Each hash function
is chosen a constant number of times in expectation, so the total
construction time is $O(n)$ in expectation.

\subsection{Using Perfect Hash to construct a MPHF}
We now have a Perfect Hash $h: S \rightarrow [4n]$. Our goal now is to
construct a minimal perfect hash $h^\prime: S \rightarrow [n]$. The
figure shows that $h$. Only $n$ of the $4n$ buckets are occupied. We
just construct a mapping of these occupied buckets to the set
$[n]$. Let this mapping be $Z$. Our MPHF is simply, $$ h^\prime (x) =
Z_{h(x)}$$.  The total space is $O(n)$ to store $h$ and $O(n)$ to
store $Z$, for $O(n)$ total.

\psscalebox{1.0 1.0} % Change this value to rescale the drawing.
{
\begin{pspicture}(0,-3.4875)(8.0,3.4875)
\psframe[linecolor=black, linewidth=0.04, dimen=outer](4.4,2.6875)(3.2,-2.9125)
\psframe[linecolor=black, linewidth=0.04, dimen=outer](4.4,2.6875)(3.2,1.8875)
\psframe[linecolor=black, linewidth=0.04, fillstyle=crosshatch, hatchwidth=0.028222222, hatchangle=0.0, hatchsep=0.1411111, dimen=outer](4.4,1.8875)(3.2,1.0875)
\psframe[linecolor=black, linewidth=0.04, fillstyle=crosshatch, hatchwidth=0.028222222, hatchangle=0.0, hatchsep=0.1411111, dimen=outer](4.4,1.0875)(3.2,0.2875)
\psframe[linecolor=black, linewidth=0.04, dimen=outer](4.4,0.2875)(3.2,-0.5125)
\psframe[linecolor=black, linewidth=0.04, dimen=outer](4.4,-2.1125)(3.2,-2.9125)
\rput(2.8,1.4875){$h(z)$}



\psframe[linecolor=black, linewidth=0.04, dimen=outer](4.4,-0.5125)(3.2,-1.3125)
\psframe[linecolor=black, linewidth=0.04, fillstyle=crosshatch, hatchwidth=0.028222222, hatchangle=0.0, hatchsep=0.1411111, dimen=outer](4.4,-1.3125)(3.2,-2.1125)
\psframe[linecolor=black, linewidth=0.04, dimen=outer](8.0,2.6875)(6.8,1.8875)
\psframe[linecolor=black, linewidth=0.04, dimen=outer](8.0,1.8875)(6.8,1.0875)
\psframe[linecolor=black, linewidth=0.04, dimen=outer](8.0,1.0875)(6.8,0.2875)
\psline[linecolor=black, linewidth=0.04](4.4,1.4875)(6.8,2.2875)(6.8,2.2875)
\psline[linecolor=black, linewidth=0.04](4.4,0.6875)(6.8,1.4875)
\psline[linecolor=black, linewidth=0.04](4.4,-1.7125)(6.8,0.6875)
\rput(6.0,1.4875){$Z$}
\rput(3.6,-3.3125){$[4n]$}
\rput(7.6,-0.1125){$[n]$}
\end{pspicture}
}


\bibliographystyle{alpha}

\begin{thebibliography}{42}

\bibitem[MU05]{MU05}
Michael~Mitzenmacher, Eli~Upfal.
\newblock Probability and Computing: Randomized Algorithms and Probabilistic Analysis
\newblock {\em Cambridge University Press}, 2005.

\end{thebibliography}

\end{document}