{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Collisions in Hashing ##"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"In computer science, *hash functions* assign a code called a *hash value* to each member of a set of individuals. It's important that each individual be assigned a unique value. If two individuals are assigned the same value, there is a *collision*, and this causes trouble in identification. Yet it is cumbersome to keep track of which hash values have and have not been assigned, as the numbers of hash values and individuals can be very large.\n",
"\n",
"What if the hash values were just assigned at random, without taking into account which of them have already been assigned? If there are a large number of distinct values and a relatively small number of individuals, then it seems reasonable to think that the chance of a collision will be small. For example, if there are 1,000 available hash values and only 5 individuals, it doesn't seem likely that you'll get a collision if you just pick a random sequence of 5 values for the 5 individuals."
]
},
{
"cell_type": "code",
"execution_count": 4,
"metadata": {
"tags": [
"remove-input",
"hide-output"
]
},
"outputs": [
{
"data": {
"text/html": [
"\n",
" VIDEO"
]
},
"execution_count": 4,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# VIDEO: Probability Model\n",
"from IPython.display import YouTubeVideo\n",
"\n",
"YouTubeVideo(\"uFLsmVFcHXw\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's make some assumptions about randomness and find the probability that there is no collision. Assume that there are $N$ hash values and $n$ individuals, and suppose your hash function is such that all $N^n$ assignments of values to individuals are equally likely. An assignment is a sequence $a_0 a_1 \\ldots a_n$ where for each $i$, individual $i$ is assigned the hash value $a_i$.\n",
"\n",
"Notice that we are assuming that each of the $n$ individuals could be assigned any of the $N$ values regardless of what has been assigned to others. This includes the truly unfortunate possibility that all $n$ individuals are assigned the same value."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### No Collisions ###\n",
"\n",
"What is the chance that there are no collisions? \n",
"\n",
"If the number of individuals $n$ is greater than the number of hash values $N$, the answer is 0. If you have more individuals than values, then you are going to have to re-use some values and therefore can't avoid collisions. \n",
"\n",
"But we are interested in the case where $n$ is quite small, so we have no problem assuming that $n \\le N$.\n",
"\n",
"If you look back to Part (i) in the example about random number generators in the previous section, you will find that it is the same as our current question, in the case where $N = 10$ and $n=2$. We can just follow the same process to get our solution here."
]
},
{
"cell_type": "code",
"execution_count": 6,
"metadata": {
"tags": [
"remove-input",
"hide-output"
]
},
"outputs": [
{
"data": {
"text/html": [
"\n",
" VIDEO"
]
},
"execution_count": 6,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# NO CODE\n",
"\n",
"# VIDEO: Collision Probability\n",
"from IPython.display import YouTubeVideo\n",
"YouTubeVideo('cD26s3DW-J8')"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"By assumption, all $N^n$ possible assignments are equally likely. Some of these assignments contain no collisions. Our job is to count how many.\n",
"\n",
"You are familiar with Python's 0-origin indexing system, which comes in handy here. We have to count the number of sequences $a_0a_1 \\ldots a_{n-1}$ where each $a_i$ is one of the $N$ hash values and all the $a_i$'s are different from each other. \n",
"\n",
"Let's do it this way:\n",
"- There are $N$ choices for $a_0$.\n",
"- For each of those choices, there are $N-1$ choices for $a_1$ because $a_1$ has to be different from $a_0$.\n",
"- Thus there are $N(N-1)$ ways of filling Place 0 and Place 1 in the sequence and avoiding a collision.\n",
"- For each of these $N(N-1)$ ways of choosing $a_0$ and $a_1$, there are $N-2$ choices for $a_2$. That's because $a_2$ has to be different from both $a_0$ and $a_1$ which are different from each other.\n",
"- Thus there are $N(N-1)(N-2)$ ways of filling Places 0, 1, and 2.\n",
"- Notice that for each $i$, the term in the product corresponding to Place $i$ is $N-i$. This makes the sequence easy to continue up to the end, that is, to Place $(n-1)$.\n",
"\n",
"$$\n",
"\\begin{align*}\n",
"P(\\mbox{no collisions}) ~ &=~ \n",
"\\frac{N(N-1)(N-2) \\cdots (N-(n-1))}{N^n} \\\\ \\\\\n",
"&=~ \\frac{N(N-1)(N-2) \\cdots (N-n+1)}{N^n}\n",
"\\end{align*}\n",
"$$\n",
"\n",
"\"Continuing the sequence\" is an informal process that needs a mathematical justification. You can prove it by the method of induction."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"```{admonition} Quick Check\n",
"Suppose you roll an ordinary six-sided die four times and keep track of the sequence of faces that appear.\n",
"\n",
"**a)** How many possible sequences are there?\n",
"\n",
"**b)** Assuming that all the sequences are equally likely, what is the probability that four distinct faces appear?\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"```{admonition} Answer\n",
":class: dropdown\n",
"(a) $6^4$ \n",
"\n",
"(b) $\\frac{6 \\times 5 \\times 4 \\times 3}{6^4}$\n",
"```"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Product of Fractions ###\n",
"\n",
"In the chance of no collisions, there are $n$ terms in the product in the numerator, and there are $n$ factors of $N$ in the denominator. This allows us to write the formula in a different way, as a product of $n$ fractions:\n",
"\n",
"$$\n",
"\\begin{align*}\n",
"P(\\mbox{no collisions}) ~ &=~\n",
"\\frac{N}{N} \\cdot \\frac{N-1}{N} \\cdot \\frac{N-2}{N} \\cdots \\frac{N-n+1}{N} \\\\ \\\\\n",
"&=~ \\prod_{i=0}^{n-1} \\frac{N-i}{N}\n",
"\\end{align*}\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The symbol $\\prod$ is the upper case Greek letter *pi*. It stands for \"product\" just as $\\sum$ stands for \"sum\"."
]
},
{
"cell_type": "markdown",
"metadata": {
"collapsed": true
},
"source": [
"And now the bad news:"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### At Least One Collision ###\n",
"Each sequence either has at least one collision, or it has no collisions. No sequence can be in both of those categories, so by the rules of proportion we have\n",
"\n",
"$$\n",
"P(\\mbox{at least one collision}) ~=~ 1 ~-~ \\prod_{i=0}^{n-1} \\frac{N-i}{N}\n",
"$$"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We have a formula. That's great! But is the answer large or is it small? It's not easy to tell just by looking at the formula. So let's start examining the answer in different ways. \n",
"\n",
"The first way is numerical. For this, we have to work with numerical values of $N$ and $n$. We'll do that in a context that has made this calculation famous."
]
}
],
"metadata": {
"anaconda-cloud": {},
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.3"
}
},
"nbformat": 4,
"nbformat_minor": 1
}