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.
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.
🎥 See More
Let’s make some assumptions about randomness and find the probability that there is no collision. Assume that there are hash values and individuals, and suppose your hash function is such that all assignments of values to individuals are equally likely. An assignment is a sequence where for each , individual is assigned the hash value .
Notice that we are assuming that each of the individuals could be assigned any of the values regardless of what has been assigned to others. This includes the truly unfortunate possibility that all individuals are assigned the same value.
1.3.1No Collisions¶
What is the chance that there are no collisions?
If the number of individuals is greater than the number of hash values , 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.
But we are interested in the case where is quite small, so we have no problem assuming that .
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 and . We can just follow the same process to get our solution here.
🎥 See More
By assumption, all possible assignments are equally likely. Some of these assignments contain no collisions. Our job is to count how many.
You are familiar with Python’s 0-origin indexing system, which comes in handy here. We have to count the number of sequences where each is one of the hash values and all the ’s are different from each other.
Let’s do it this way:
There are choices for .
For each of those choices, there are choices for because has to be different from .
Thus there are ways of filling Place 0 and Place 1 in the sequence and avoiding a collision.
For each of these ways of choosing and , there are choices for . That’s because has to be different from both and which are different from each other.
Thus there are ways of filling Places 0, 1, and 2.
Notice that for each , the term in the product corresponding to Place is . This makes the sequence easy to continue up to the end, that is, to Place .
“Continuing the sequence” is an informal process that needs a mathematical justification. You can prove it by the method of induction.
Answer
(a) 64
(b)
1.3.2Product of Fractions¶
In the chance of no collisions, there are terms in the product in the numerator, and there are factors of in the denominator. This allows us to write the formula in a different way, as a product of fractions:
The symbol is the upper case Greek letter pi. It stands for “product” just as stands for “sum”.
And now the bad news:
1.3.3At Least One Collision¶
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
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.
The first way is numerical. For this, we have to work with numerical values of and . We’ll do that in a context that has made this calculation famous.