Discrete MathematicsProbability, Conditioning and Expectation9 of 9
Probability, Conditioning and Expectation · 9

Randomized Algorithms and Hashing

When an algorithm follows a fixed, predictable script, an awkward pattern in the data, or a deliberate attacker, can force it into its worst case on every step.

1

Beating the Adversary and Las Vegas Algorithms

Suppose we want to pick a number from the middle half of an unsorted list of 1,000,0001{,}000{,}000 numbers without taking the time to sort the list.

Beating the Adversary and Las Vegas Algorithms
rule: always pick the first\text{rule: always pick the first}
1,000,000 numbers1{,}000{,}000\ \text{numbers}
Text solution
Relate the first try to the remaining triesOn the first random pick, we either hit a green number with probability p\good{p} and stop, or hit a red number with probability 1−p\miss{1 - p} and still expect EE more tries from the start.
E\displaystyle E =1⏟first try+(1−p)E⏟if we miss, E more tries\displaystyle = \underbrace{1}_{\text{first try}} + \underbrace{\miss{(1 - p)}E}_{\text{if we miss, } E \text{ more tries}}
Solve for the expected number of tries EEExpand the right-hand side and subtract EE from both sides so only the success probability p\good{p} remains.
E=1+E−pEpE=1E=1p\displaystyle \begin{aligned} E &= 1 + E - \good{p}E \\ \good{p}E &= 1 \\ E &= \frac{1}{\good{p}} \end{aligned}
Evaluate for the middle halfBecause half of the list sits in the middle two quarters, each random pick succeeds with probability p=12\good{p} = \tfrac{1}{2}.
E\displaystyle E =112\displaystyle = \frac{1}{\good{\tfrac{1}{2}}} =2 tries\displaystyle = 2 \text{ tries}

So a Las Vegas search finds a middle-half number in 22 tries on average (or 1010 tries if we narrow the target to the middle tenth where p=110\good{p} = \tfrac{1}{10}), and it never returns a wrong number regardless of how the list is ordered.

An adversary can always move the smallest numbers under a fixed rule's arrows to make it fail, while a random pick still takes 22 tries on average because the fraction of green numbers on the board never changes.

Rearranging the list can trap any fixed rule on the smallest numbers, while a random pick still takes 2 tries on average because half the board stays green in every order.
2

Monte Carlo Algorithms and the Fermat Primality Test

A Las Vegas loop works when we can immediately verify a good item, but it doesn't work when we have to test a yes-or-no claim about an entire massive input, such as checking whether a box of a billion numbers is all zeros, or whether a 300300-digit number nn is prime. A Monte Carlo algorithm checks a fixed number of kk random samples for a witness that disproves the claim, and concludes the claim is probably true if all kk samples pass.

To test whether a giant integer nn is prime without factoring it, we use Fermat's Little Theorem from modular arithmetic: if n\given{n} is prime, then an−1≡1(modn)\thenpart{a^{n-1} \equiv 1 \pmod{n}} for every base a∈{1,2,…,n−1}a \in \{1, 2, \dots, n - 1\}. Taking the contrapositive of that conditional gives a witness test for compositeness: any base aa with an−1 mod n≠1\thenpart{a^{n-1} \bmod n} \neq 1 proves immediately that n\given{n} is composite.

Monte Carlo Algorithms and the Fermat Primality Test
1,000,000,000numbers\begin{gathered} 1{,}000{,}000{,}000 \\ \text{numbers} \end{gathered}
claim: every number is 0\htmlClass{lc-blue}{\text{claim: every number is } 0}
check all:  1 houryou have:  1 ms\begin{aligned} \text{check all:}&\ \ 1\ \text{hour} \\[2pt] \text{you have:}&\ \ 1\ \text{ms} \end{aligned}
Text solution
Test n=9n = 9 with base a=2\witness{a = 2}Compute 29−1 mod 9\witness{2}^{9-1} \bmod 9 to see whether a=2\witness{a = 2} breaks Fermat's condition.
29−1 mod 9=28 mod 9=256 mod 9=4≠1(a=2 is a Fermat witness⏟proves 9 is composite)\displaystyle \begin{gathered} \begin{aligned} \witness{2}^{9-1} \bmod 9 &= \witness{2}^8 \bmod 9 \\ &= 256 \bmod 9 \\ &= 4 \neq 1 \end{aligned} \\ (\underbrace{\witness{a = 2} \text{ is a Fermat witness}}_{\text{proves } 9 \text{ is composite}}) \end{gathered}
Test n=9n = 9 with base a=8\liar{a = 8}Rewrite 8≡−1(mod9)\liar{8} \equiv \liar{-1} \pmod{9} before raising it to the 88th power.
89−1 mod 9=(−1)8 mod 9=1(a=8 is a Fermat liar⏟fools the test for 9)\displaystyle \begin{gathered} \begin{aligned} \liar{8}^{9-1} \bmod 9 &= (\liar{-1})^8 \bmod 9 \\ &= 1 \end{aligned} \\ (\underbrace{\liar{a = 8} \text{ is a Fermat liar}}_{\text{fools the test for } 9}) \end{gathered}
Bound the error after kk random basesBecause at most half of the bases are liars for any non-Carmichael composite nn, each independent test is fooled with probability at most 12\fooled{\tfrac{1}{2}}.
Pr⁡(fooled all k times)\displaystyle \Pr(\text{fooled all } k \text{ times}) ≤12×12×⋯×12⏟k independent tries\displaystyle \le \underbrace{\fooled{\frac{1}{2}} \times \fooled{\frac{1}{2}} \times \cdots \times \fooled{\frac{1}{2}}}_{k \text{ independent tries}} =(12)k\displaystyle = \fooled{\left(\frac{1}{2}\right)^k}

So a single witness (an−1 mod n≠1\thenpart{a^{n-1} \bmod n} \neq 1) proves n\given{n} is composite with 100%100\% certainty, while 5050 random bases that all return 11 leave at most a (12)50≈10−15\fooled{\left(\tfrac{1}{2}\right)^{50}} \approx 10^{-15} chance of being fooled by a composite number that isn't a Carmichael number.

3

Hash Functions and the Birthday Paradox

Now suppose we want to store a small set of records with huge keys, like 99-digit ID numbers, inside a small array of mm slots (numbered 00 to m−1m - 1) so we can jump straight to any record's slot in one step. The simplest hash function compresses any large key xx into a valid slot number by taking its remainder modulo mm:

h(x)=x mod m⏟maps any integer x into {0,1,…,m−1}\displaystyle \underbrace{\hash{h}(x) = x \hash{\bmod m}}_{\text{maps any integer } x \text{ into } \{0, 1, \dots, m - 1\}}

When two different keys x≠yx \neq y land in the same slot (h(x)=h(y)\hash{h}(x) = \hash{h}(y)), we get a collision.

Hash Functions and the Birthday Paradox
array\text{array}
0\htmlClass{lc-mute}{0}
1\htmlClass{lc-mute}{1}
2\htmlClass{lc-mute}{2}
3\htmlClass{lc-mute}{3}
4\htmlClass{lc-mute}{4}
5\htmlClass{lc-mute}{5}
6\htmlClass{lc-mute}{6}
Text solution
Multiply the collision-free probabilities for n=4n = 4 items in m=7m = 7 slotsEach new item must land in one of the remaining empty slots missed by the items placed before it.
Pr⁡(no collisions)=mm×m−1m×⋯×m−n+1m=77×67×57×47=120343≈0.35\displaystyle \begin{aligned} &\Pr(\nocollide{\text{no collisions}}) \\ &\quad = \frac{\nocollide{m}}{m} \times \frac{\nocollide{m - 1}}{m} \times \cdots \\ &\qquad \times \frac{\nocollide{m - n + 1}}{m} \\ &\quad = \frac{\nocollide{7}}{7} \times \frac{\nocollide{6}}{7} \times \frac{\nocollide{5}}{7} \times \frac{\nocollide{4}}{7} \\ &\quad = \frac{120}{343} \approx 0.35 \end{aligned}
Subtract from 11 to find the probability of at least one collisionAny outcome that is not completely collision-free has at least one pair of items sharing a slot.
Pr⁡(at least one collision)=1−Pr⁡(no collisions)=1−120343=223343≈65%\displaystyle \begin{aligned} &\Pr(\collide{\text{at least one collision}}) \\ &\quad = 1 - \Pr(\nocollide{\text{no collisions}}) \\ &\quad = 1 - \frac{120}{343} \\ &\quad = \frac{223}{343} \approx 65\% \end{aligned}

So even when a 77-slot table is nearly half empty with 44 items, a collision is already 65%65\% likely (and with m=365m = 365 days, just n=23n = 23 people give a 50.7%50.7\% chance of a shared birthday).

4

Separate Chaining, Linear Probing, and Load Factor

Because collisions happen long before a hash table is full, every hash table needs a collision rule and a measure of how crowded its slots are. The load factor α=nm\expected{\alpha} = \tfrac{n}{m} measures the average number of stored keys per slot.

Separate Chaining, Linear Probing, and Load Factor
m=7,h(x)=x mod 7m = 7, \quad \htmlClass{lc-violet}{h}(x) = x \htmlClass{lc-violet}{\bmod 7}
insert\text{insert}
1010
1919
2626
1414
3333
0\htmlClass{lc-mute}{0}
1\htmlClass{lc-mute}{1}
2\htmlClass{lc-mute}{2}
3\htmlClass{lc-mute}{3}
4\htmlClass{lc-mute}{4}
5\htmlClass{lc-mute}{5}
6\htmlClass{lc-mute}{6}
h(x)=x mod 7\htmlClass{lc-violet}{h}(x) = x \htmlClass{lc-violet}{\bmod 7}
Separate chaining\text{Separate chaining}
0\htmlClass{lc-mute}{0}
1\htmlClass{lc-mute}{1}
2\htmlClass{lc-mute}{2}
3\htmlClass{lc-mute}{3}
4\htmlClass{lc-mute}{4}
5\htmlClass{lc-mute}{5}
6\htmlClass{lc-mute}{6}
1414
1010
1919
2626
3333
Linear probing\text{Linear probing}
primary cluster (slots 5, 6, 0, 1)\htmlClass{lc-orange}{\text{primary cluster (slots 5, 6, 0, 1)}}
1414
0\htmlClass{lc-mute}{0}
3333
1\htmlClass{lc-mute}{1}
2\htmlClass{lc-mute}{2}
1010
3\htmlClass{lc-mute}{3}
4\htmlClass{lc-mute}{4}
1919
5\htmlClass{lc-mute}{5}
2626
6\htmlClass{lc-mute}{6}
Text solution
Insert 10,19,26,14,3310, 19, 26, 14, 33 into m=7m = 7 slots using linear probingStart at h(x)=x mod 7\hash{h}(x) = x \hash{\bmod 7} and step right through (h(x)+i) mod 7(\hash{h}(x) + i) \bmod 7 whenever a slot is already taken.
10 mod 7=3→slot 319 mod 7=5→slot 526 mod 7=5→slot 5 (taken)→slot 614 mod 7=0→slot 033 mod 7=5→slots 5,6,0 (taken)→slot 1\displaystyle \begin{aligned} 10 \bmod 7 &= 3 \rightarrow \text{slot } 3 \\ 19 \bmod 7 &= 5 \rightarrow \text{slot } 5 \\ 26 \bmod 7 &= 5 \\ &\quad \rightarrow \collide{\text{slot } 5 \text{ (taken)}} \\ &\quad \rightarrow \text{slot } 6 \\ 14 \bmod 7 &= 0 \rightarrow \text{slot } 0 \\ 33 \bmod 7 &= 5 \\ &\quad \rightarrow \collide{\text{slots } 5, 6, 0 \text{ (taken)}} \\ &\quad \rightarrow \text{slot } 1 \end{aligned}
Find the expected collisions for a new key x\newkey{x}Define an indicator random variable Ii=1I_i = 1 if existing key yiy_i lands in x\newkey{x}'s slot (so E[Ii]=1m\expected{E[I_i]} = \pairchance{\tfrac{1}{m}}) and 00 otherwise, then sum over all nn keys by linearity of expectation.
E[collisions with x]=E[I1+I2+⋯+In]=E[I1]+E[I2]+⋯+E[In]=1m+1m+⋯+1m⏟n existing keys=nm=α⏟load factor\displaystyle \begin{aligned} &E[\text{collisions with } \newkey{x}] \\ &\quad = E[I_1 + I_2 + \cdots + I_n] \\ &\quad = \expected{E[I_1]} + \expected{E[I_2]} + \cdots + \expected{E[I_n]} \\ &\quad = \underbrace{\pairchance{\frac{1}{m}} + \pairchance{\frac{1}{m}} + \cdots + \pairchance{\frac{1}{m}}}_{n \text{ existing keys}} \\ &\quad = \underbrace{\frac{n}{m} = \expected{\alpha}}_{\text{load factor}} \end{aligned}

So keeping α≤1\expected{\alpha} \le 1 by doubling mm whenever nn reaches mm keeps the expected chain length at most 11, giving O(1)O(1) average lookup time.

When colliding keys step right into a contiguous cluster, what happens when we delete a key from the middle of that cluster?

In linear probing, colliding keys step right into clusters, and deleted keys leave markers that searches step over and new keys reuse.
5

Universal Hashing

Our expected chain length of α=nm\expected{\alpha} = \tfrac{n}{m} relied on the assumption that any two keys collide with probability 1m\pairchance{\tfrac{1}{m}}. A single fixed hash function like h(x)=x mod 7\hash{h}(x) = x \hash{\bmod 7} cannot guarantee this on every input: if an adversary feeds the server multiples of 77 (7,14,21,28,357, 14, 21, 28, 35), every key hashes to slot 0\collide{0} and slows lookups from O(1)O(1) to O(n)\collide{O(n)}.

To defeat the adversary, universal hashing picks a hash function at random at startup from a family H\hash{\mathcal{H}} designed so that any two distinct keys collide with probability at most 1m\pairchance{\tfrac{1}{m}}.

Universal Hashing
each key lands in any slotwith probability 1m, independently\htmlClass{lc-blue}{\begin{gathered} \text{each key lands in any slot} \\ \text{with probability } \tfrac{1}{m}\text{, independently} \end{gathered}}
h(x)=x mod 7\htmlClass{lc-violet}{h}(x) = x \htmlClass{lc-violet}{\bmod 7}
Adversary: 7,14,21,28,35,42,49,…\text{Adversary: } 7, 14, 21, 28, 35, 42, 49, \dots
0\htmlClass{lc-mute}{0}
1\htmlClass{lc-mute}{1}
2\htmlClass{lc-mute}{2}
3\htmlClass{lc-mute}{3}
4\htmlClass{lc-mute}{4}
5\htmlClass{lc-mute}{5}
6\htmlClass{lc-mute}{6}
Text solution
Pick a random function from the universal family at startupChoose a prime pp larger than all possible keys, and draw a secret random multiplier a∈{1,2,…,p−1}a \in \{1, 2, \dots, p - 1\} and shift b∈{0,1,…,p−1}b \in \{0, 1, \dots, p - 1\}.
ha,b(x)\displaystyle \hash{h_{a,b}}(x) =((ax+b) mod p) mod m\displaystyle = ((ax + b) \hash{\bmod p}) \hash{\bmod m}
Bound the collision probability for any pair of distinct keysBecause aa and bb are chosen uniformly at random before any input arrives, even hand-picked multiples of mm are scrambled modulo pp before being reduced modulo mm.
Pr⁡ha,b∈H(ha,b(x)=ha,b(y))\displaystyle \Pr_{\hash{h_{a,b}} \in \hash{\mathcal{H}}}\bigl(\hash{h_{a,b}}(x) = \hash{h_{a,b}}(y)\bigr) ≤1mfor all x≠y\displaystyle \le \pairchance{\frac{1}{m}} \quad \text{for all } x \neq y

So for every possible input list, each indicator variable satisfies E[Ii]≤1m\expected{E[I_i]} \le \pairchance{\tfrac{1}{m}}, and the expected number of collisions in any slot stays at most α=nm\expected{\alpha} = \tfrac{n}{m}.

Multiplying by aa and shifting by bb modulo pp breaks up the spacing of the multiples 7,14,21,28,357, 14, 21, 28, 35, so for almost every pair (a,b)(a, b) they no longer all land in the same slot.

Even when every input key is a multiple of 7, almost every hash function in the family keeps them from piling into one slot.

Key concepts

When an algorithm or data structure faces unknown or adversarial inputs, we choose how to use randomness based on whether we can afford a tiny chance of error or only a variable runtime:

Las Vegas vs. Monte Carlo

Use a Las Vegas algorithm when a candidate answer can be checked directly and the final answer must be 100%100\% right; if each try succeeds with probability p\good{p}, the expected number of tries is E=1pE = \tfrac{1}{\good{p}}, and the probability of missing kk times in a row is (1−p)k\miss{(1 - p)^k} (for p=14\good{p} = \tfrac{1}{4}, E=4E = 4 tries and 33 misses have probability (34)3\miss{\left(\tfrac{3}{4}\right)^3}). Use a Monte Carlo algorithm when checking the entire space is too slow and you test kk random samples for a witness instead: a single witness proves the claim false with 100%100\% certainty (such as 28 mod 9=4≠1\witness{2}^8 \bmod 9 = 4 \neq 1 proving 99 is composite), whereas kk passes leave an error probability of at most (12)k\fooled{\left(\tfrac{1}{2}\right)^k}. Do not use Fermat's test to prove a number prime with 100%100\% certainty: a single base returning an−1 mod n=1a^{n-1} \bmod n = 1 can be a Fermat liar like 88 mod 9=1\liar{8}^8 \bmod 9 = 1.

Collisions and the Birthday Paradox

A hash function h(x)=x mod m\hash{h}(x) = x \hash{\bmod m} maps keys into mm slots. You only need n=m+1n = m + 1 keys for the Pigeonhole Principle to guarantee a collision with probability 11, but for nn random keys the exact collision probability is

Pr⁡(at least one collision)\displaystyle \Pr(\collide{\text{at least one collision}}) =1−mm⋅m−1m⋯m−n+1m\displaystyle = 1 - \frac{\nocollide{m}}{m}\cdot\frac{\nocollide{m - 1}}{m}\cdots\frac{\nocollide{m - n + 1}}{m}
which already passes 50%50\% near n≈mn \approx \sqrt{m}.

Handling collisions and load factor

Separate chaining stores colliding keys in a linked list at slot h(x)\hash{h}(x), whereas linear probing steps through (h(x)+i) mod m(\hash{h}(x) + i) \bmod m inside the array (leaving a `DELETED` marker on deletion so searches step over it while insertions overwrite it). Under uniform hashing (or universal hashing ha,b(x)=((ax+b) mod p) mod m\hash{h_{a,b}}(x) = ((ax + b) \hash{\bmod p}) \hash{\bmod m}, which keeps Pr⁡(ha,b(x)=ha,b(y))≤1m\Pr(\hash{h_{a,b}}(x) = \hash{h_{a,b}}(y)) \le \pairchance{\tfrac{1}{m}} even against adversarial multiples of mm), linearity of expectation gives an expected chain length of at most the load factor α=nm\expected{\alpha} = \tfrac{n}{m}.

6

Practice

1

You want to pick a number strictly between the 40th and 65th percentiles (a band holding 25%25\% of the data) from an unsorted array of 1,000,0001{,}000{,}000 distinct numbers using a Las Vegas algorithm that picks an index uniformly at random and repeats until it hits a number in that range. How many tries does the algorithm take on average, and what is the probability that it still has not found a valid number after 33 tries?

Find the success probability p\good{p} of a single pick from the percentile range, then use E=1pE = \tfrac{1}{\good{p}} for the average tries and multiply the single-try miss probability 1−p\miss{1 - p} across 33 tries.

Find the expected number of triesSince 25%25\% of the array lies strictly between the 40th and 65th percentiles, each random pick succeeds with probability p=14\good{p} = \tfrac{1}{4} (the total size 1,000,0001{,}000{,}000 cancels out of the fraction).
E\displaystyle E =1p\displaystyle = \frac{1}{\good{p}} =114\displaystyle = \frac{1}{\good{\tfrac{1}{4}}} =4 tries\displaystyle = 4 \text{ tries}
Find the probability of missing 33 times in a rowEach try misses the target range with probability 1−p=34\miss{1 - p} = \tfrac{3}{4}. Be careful not to raise the success probability p=14\good{p} = \tfrac{1}{4} to the 33rd power, which would give (14)3=164\wrong{\left(\tfrac{1}{4}\right)^3 = \tfrac{1}{64}} (the chance of hitting three good numbers in a row).
Pr⁡(3 misses)\displaystyle \Pr(\text{3 misses}) =(1−p)3\displaystyle = \miss{(1 - p)^3} =(34)3\displaystyle = \miss{\left(\frac{3}{4}\right)^3} =2764≈42.2%\displaystyle = \frac{27}{64} \approx 42.2\%
The algorithm takes 44 tries on average, and the probability it has not found a valid number after 33 tries is 2764\frac{27}{64} (about 42.2%42.2\%).
2

A mystery box contains 10,000,00010{,}000{,}000 numbers. Someone claims every number in the box is 00, and you know that if the box is corrupted, at least half of its numbers are non-zero. Using a Monte Carlo algorithm that inspects kk randomly chosen numbers (with replacement), what can you conclude if the very first number you pull out is 99, and what is the smallest number of tries kk needed so that if all kk tries return 00, the probability you were fooled by a corrupted box is less than 11 in 500500?

Distinguish between seeing a non-zero witness and seeing only zeros, then compare powers of (12)k\fooled{\left(\tfrac{1}{2}\right)^k} against 1500\tfrac{1}{500}.

Conclude from drawing a 9\witness{9}Because 9≠0\witness{9} \neq 0, it is a witness that disproves the claim immediately with 100%100\% certainty (do not say it is only 50%50\% certain; random error only applies when the sample returns 00).
Solve for the number of tries kkIf the box is corrupted, at most 12\fooled{\tfrac{1}{2}} of the numbers are 00, so kk draws of 00 happen with probability at most (12)k\fooled{\left(\tfrac{1}{2}\right)^k}. Checking powers of 22, watch out for stopping at k=8k = 8, which gives (12)8=1256>1500\fooled{\left(\tfrac{1}{2}\right)^8} = \tfrac{1}{256} > \tfrac{1}{500}.
(12)8=1256>1500(12)9=1512<1500\displaystyle \begin{aligned} \fooled{\left(\frac{1}{2}\right)^8} &= \frac{1}{256} > \frac{1}{500} \\ \fooled{\left(\frac{1}{2}\right)^9} &= \frac{1}{512} < \frac{1}{500} \end{aligned}
Drawing 9\witness{9} proves with 100%100\% certainty that the box is not all zeros, and you need k=9k = 9 tries to push the error probability below 11 in 500500.
3

Use the Fermat Primality Test on the composite number n=15n = 15 by computing a14 mod 15a^{14} \bmod 15 for the bases a=2a = 2 and a=4a = 4. Which of the two bases is a Fermat witness for 1515, and which is a Fermat liar?

Notice that 24=16≡1(mod15)2^4 = 16 \equiv 1 \pmod{15} and 42=16≡1(mod15)4^2 = 16 \equiv 1 \pmod{15} to simplify the 1414th powers quickly.

Test base a=2\witness{a = 2}Use 24=16≡1(mod15)\witness{2}^4 = 16 \equiv 1 \pmod{15} to reduce 214 mod 15\witness{2}^{14} \bmod 15. (Make sure to use the exponent n−1=14n - 1 = 14 rather than n=15n = 15.)
214 mod 15=((24)3⋅22) mod 15≡(13⋅4) mod 15=4≠1\displaystyle \begin{aligned} \witness{2}^{14} \bmod 15 &= \bigl((\witness{2}^4)^3 \cdot \witness{2}^2\bigr) \bmod 15 \\ &\equiv (1^3 \cdot 4) \bmod 15 \\ &= 4 \neq 1 \end{aligned}
Test base a=4\liar{a = 4}Use 42=16≡1(mod15)\liar{4}^2 = 16 \equiv 1 \pmod{15} to reduce 414 mod 15\liar{4}^{14} \bmod 15.
414 mod 15=(42)7 mod 15≡17 mod 15=1\displaystyle \begin{aligned} \liar{4}^{14} \bmod 15 &= (\liar{4}^2)^7 \bmod 15 \\ &\equiv 1^7 \bmod 15 \\ &= 1 \end{aligned}
Classify each baseDo not mix up the two terms: a result different from 11 is a witness that proves 1515 is composite, whereas a result of 11 is a liar that makes 1515 look prime.
Base a=2\witness{a = 2} is a Fermat witness for 1515, and base a=4\liar{a = 4} is a Fermat liar.
4

Suppose n=4n = 4 keys are hashed one by one into a table with m=10m = 10 slots, where each key independently picks one of the 1010 slots uniformly at random. What is the exact probability of at least one collision, and how many keys must be inserted before the Pigeonhole Principle guarantees a collision with probability 11?

Multiply the probabilities that each of the 44 keys avoids the previous keys, and remember to subtract that product from 11.

Find the probability of no collisionsMultiply the fraction of empty slots available to each of the 44 keys in turn.
Pr⁡(no collisions)=1010×910×810×710=5041000=0.504\displaystyle \begin{aligned} &\Pr(\nocollide{\text{no collisions}}) \\ &\quad = \frac{10}{10} \times \frac{9}{10} \times \frac{8}{10} \times \frac{7}{10} \\ &\quad = \frac{504}{1000} \\ &\quad = 0.504 \end{aligned}
Subtract from 11 for at least one collisionDo not stop at 0.5040.504, which is the probability of zero collisions.
Pr⁡(at least one collision)\displaystyle \Pr(\collide{\text{at least one collision}}) =1−0.504\displaystyle = 1 - 0.504 =0.496(49.6%)\displaystyle = 0.496 \quad (49.6\%)
Apply the Pigeonhole PrincipleFilling all 1010 slots takes 1010 keys without forcing a collision, so you need m+1=11m + 1 = 11 keys (not 1010) to guarantee a collision.
The probability of at least one collision is 0.4960.496 (49.6%49.6\%), and 1111 keys guarantee a collision with probability 11.
5

An initially empty hash table has m=7m = 7 slots (numbered 00 to 66) and uses h(x)=x mod 7h(x) = x \bmod 7 with linear probing. Insert the keys 12,5,19,23,3312, 5, 19, 23, 33 in that order, and then delete 1919. Give the final contents of slots 00 through 66, and list the exact sequence of slots checked when searching for 3333 and when searching for 4040.

Compute x mod 7x \bmod 7 for each key, wrap around from slot 66 to slot 00 when probing, and place a `DELETED` marker when removing 1919.

Insert the five keys and delete 1919Since 12,5,19,12, 5, 19, and 3333 all have x mod 7=5x \bmod 7 = 5 while 23 mod 7=223 \bmod 7 = 2, they fill slots 5,6,0,2,15, 6, 0, 2, 1 in order, and deleting 1919 replaces slot 00 with `DELETED`.
12 mod 7=5→slot 55 mod 7=5→slot 619 mod 7=5→slot 0(later becomes DELETED)23 mod 7=2→slot 233 mod 7=5→slot 1\displaystyle \begin{aligned} 12 \bmod 7 &= 5 \rightarrow \text{slot } 5 \\ 5 \bmod 7 &= 5 \rightarrow \text{slot } 6 \\ 19 \bmod 7 &= 5 \rightarrow \text{slot } 0 \\ &\quad (\text{later becomes } \texttt{DELETED}) \\ 23 \bmod 7 &= 2 \rightarrow \text{slot } 2 \\ 33 \bmod 7 &= 5 \rightarrow \text{slot } 1 \end{aligned}
Trace the searches for 3333 and 4040Both start at 33 mod 7=40 mod 7=533 \bmod 7 = 40 \bmod 7 = 5. Do not treat slot 00 as empty when 1919 is deleted (which would stop both searches at slot 00 and miss 3333), and do not stop the search for 4040 at slot 22 without checking the empty slot 33 that proves 4040 is not in the table.
The final table is `[0: DELETED, 1: 33, 2: 23, 3: empty, 4: empty, 5: 12, 6: 5]`; searching for 3333 checks slots 5,6,0,15, 6, 0, 1 (found), and searching for 4040 checks slots 5,6,0,1,2,35, 6, 0, 1, 2, 3 (not found).
6

A hash table using separate chaining has m=50m = 50 slots and currently stores n=125n = 125 keys. Assuming each key lands in any slot with probability 150\tfrac{1}{50}, what is the load factor α\alpha, how many existing keys is a new key xx expected to collide with, and how many times must the table double its number of slots so that α≤1\alpha \le 1?

Compute α=nm\expected{\alpha} = \tfrac{n}{m} (items divided by slots), and check α\expected{\alpha} after each doubling of mm.

Compute the load factor and expected collisionsDivide the n=125n = 125 keys by the m=50m = 50 slots (do not invert the fraction to mn=50125=0.4\wrong{\tfrac{m}{n} = \tfrac{50}{125} = 0.4}). By linearity of expectation over the 125125 indicator variables, the expected number of collisions for x\newkey{x} equals α\expected{\alpha}.
α\displaystyle \expected{\alpha} =nm\displaystyle = \frac{n}{m} =12550\displaystyle = \frac{125}{50} =2.5 expected collisions\displaystyle = 2.5 \text{ expected collisions}
Double mm until α≤1\alpha \le 1Doubling once gives m=100m = 100 slots with α=125100=1.25>1\expected{\alpha} = \tfrac{125}{100} = 1.25 > 1, so one doubling is not enough; doubling a second time gives m=200m = 200 slots.
αnew\displaystyle \expected{\alpha_{\text{new}}} =125200\displaystyle = \frac{125}{200} =0.625\displaystyle = 0.625 ≤1\displaystyle \le 1
The load factor is α=2.5\expected{\alpha} = 2.5, the new key is expected to collide with 2.52.5 keys, and the table must double 22 times.
7

To attack a table with m=5m = 5 slots and fixed hash function h(x)=x mod 5h(x) = x \bmod 5, an adversary submits the keys 10,15,2010, 15, 20, which all collide in slot 00. Suppose the server instead uses the universal family ha,b(x)=((ax+b) mod 23) mod 5h_{a,b}(x) = ((ax + b) \bmod 23) \bmod 5 with prime p=23p = 23, and randomly picks a=7a = 7 and b=1b = 1 at startup. Which slots do 10,15,2010, 15, 20 land in under h7,1h_{7,1}, and before (a,b)(a, b) is revealed, what upper bound does universal hashing guarantee on the probability that 1010 and 1515 collide?

For each key, compute (7x+1) mod 23(7x + 1) \bmod 23 first before taking the remainder modulo 55.

Evaluate h7,1(x)=((7x+1) mod 23) mod 5\hash{h_{7,1}}(x) = ((7x + 1) \hash{\bmod 23}) \hash{\bmod 5}Always reduce modulo p=23p = 23 before reducing modulo m=5m = 5 (skipping the  mod  23\bmod\ 23 step would leave (7x+1) mod 5=1\wrong{(7x + 1) \bmod 5 = 1} for every multiple of 55).
h7,1(10)=(71 mod 23) mod 5=2 mod 5=2h7,1(15)=(106 mod 23) mod 5=14 mod 5=4h7,1(20)=(141 mod 23) mod 5=3 mod 5=3\displaystyle \begin{aligned} \hash{h_{7,1}}(10) &= (71 \bmod 23) \bmod 5 \\ &= 2 \bmod 5 = 2 \\ \hash{h_{7,1}}(15) &= (106 \bmod 23) \bmod 5 \\ &= 14 \bmod 5 = 4 \\ \hash{h_{7,1}}(20) &= (141 \bmod 23) \bmod 5 \\ &= 3 \bmod 5 = 3 \end{aligned}
State the universal collision boundThe universal bound depends on the number of table slots m=5m = 5, not the prime p=23p = 23.
Pr⁡(ha,b(10)=ha,b(15))\displaystyle \Pr\bigl(\hash{h_{a,b}}(10) = \hash{h_{a,b}}(15)\bigr) ≤1m\displaystyle \le \pairchance{\frac{1}{m}} =15\displaystyle = \frac{1}{5}
The keys 10,15,2010, 15, 20 land in slots 2,4,2, 4, and 33, and universal hashing guarantees a collision probability of at most 15\frac{1}{5}.