Sub topic 21
Load Factor and Expected Probe Counts
This chapter assumes you know how chaining and open addressing place keys (linear, quadratic, and double hashing), the idea of a collision, and basic logarithms and geometric series. It gives the formulas GATE questions use to estimate search cost from the load factor, and shows where each formula comes from and where it stops being valid.
The load factor
A hash table has m slots and holds n keys. The load factor is
α = n / m
It measures how full the table is. The two collision strategies treat α differently:
- With chaining, α can be any non-negative number. A table with m = 10 and n = 25 has α = 2.5, which means the average chain has 2.5 keys.
- With open addressing, every key sits in its own slot, so n ≤ m and 0 ≤ α ≤ 1. At α = 1 the table is full and an insertion cannot succeed.
A probe is one inspection of a table slot. For chaining, the cost is usually counted in key comparisons along a chain. For open addressing, it is counted in slots examined.
Assumption: simple uniform hashing
The standard formulas assume simple uniform hashing. Each key is equally likely to hash to any of the m slots, independently of the other keys. For open addressing the assumption is stronger, called uniform hashing. The probe sequence of each key is equally likely to be any of the m! permutations of the slots.
Real schemes only approximate this. Linear probing does not satisfy it, which is why its formulas differ from the ideal ones.
Expected cost under chaining
Under simple uniform hashing, the expected length of any one chain is α. Searching has two parts: computing the hash and walking the chain.
| Search outcome | Expected comparisons | Reason |
|---|---|---|
| Unsuccessful | α | The whole chain must be scanned. |
| Successful | about 1 + α/2 | On average, half of the chain is scanned before the key is found. |
Including the O(1) cost of computing the hash and reaching the slot, both searches take Θ(1 + α). If n = O(m), then α = O(1) and the expected cost per operation is constant. This is the usual justification for keeping m proportional to n.
The successful-search figure is more precise than "half the chain". Consider a key inserted at the end of its chain. The number of comparisons to find it is one more than the number of keys inserted before it into the same chain. Averaging over all n keys gives 1 + (n − 1)/(2m), which is about 1 + α/2.
Expected cost under open addressing
With uniform hashing and α < 1, the expected number of probes is:
| Operation | Expected probes |
|---|---|
| Unsuccessful search (also insertion) | at most 1 / (1 − α) |
| Successful search | at most (1/α) · ln(1 / (1 − α)) |
Why unsuccessful search costs 1/(1 − α)
An unsuccessful search keeps probing until it hits an empty slot. At each probe, the chance that the slot is occupied is about α, so the chance it is empty is 1 − α. The number of probes until the first empty slot is geometric, with expected value 1 / (1 − α).
Some values show how quickly the cost grows:
| α | Unsuccessful (1/(1 − α)) | Successful ((1/α) ln(1/(1 − α))) |
|---|---|---|
| 0.50 | 2.00 | 1.39 |
| 0.75 | 4.00 | 1.85 |
| 0.90 | 10.00 | 2.56 |
| 0.99 | 100.00 | 4.65 |
An insertion costs the same as an unsuccessful search, because it probes until it finds an empty slot. Note that the successful search is always cheaper than the unsuccessful one at the same α.
Why successful search is cheaper
Finding a key repeats the probe sequence that inserted it. A key that was inserted when the table held i keys needed about 1 / (1 − i/m) probes. Averaging this over i from 0 to n − 1 gives the integral that yields (1/α) ln(1/(1 − α)).
Linear probing is worse
Linear probing suffers from primary clustering. Occupied slots form long runs, and a key that hashes into a run must walk to its end. Larger runs are also more likely to grow, because more keys land in them. Knuth's classic results for linear probing are:
- Successful search: about ½ (1 + 1 / (1 − α))
- Unsuccessful search: about ½ (1 + 1 / (1 − α)²)
At α = 0.75, a successful search takes about 2.5 probes and an unsuccessful one about 8.5, compared with 4 under uniform hashing. Quadratic probing avoids primary clustering but has secondary clustering, because keys with the same initial slot follow the same sequence. Double hashing comes closest to uniform hashing.
Worked example: tracing and counting probes
Insert the keys 12, 25, 35, 26, 15 into a table of size m = 10 with h(k) = k mod 10 and linear probing. Count the probes for each insertion.
def insert_with_count(table, key):
m = len(table)
for i in range(m):
slot = (key % m + i) % m
if table[slot] is None:
table[slot] = key
return slot, i + 1
raise OverflowError("table full")
table = [None] * 10
total = 0
for k in [12, 25, 35, 26, 15]:
slot, probes = insert_with_count(table, k)
total += probes
print(k, "-> slot", slot, "probes", probes)
print("table:", table)
print("load factor:", 5 / 10)
print("average insert probes:", total / 5)The output is:
12 -> slot 2 probes 1
25 -> slot 5 probes 1
35 -> slot 6 probes 2
26 -> slot 7 probes 2
15 -> slot 8 probes 4
table: [None, None, 12, None, None, 25, 35, 26, 15, None]
load factor: 0.5
average insert probes: 2.0Key 35 hashes to slot 5, which holds 25, so it moves to slot 6. Key 26 hashes to slot 6, which now holds 35, so it takes slot 7. Key 15 hashes to slot 5 and must pass 25, 35, and 26 before reaching slot 8. This is primary clustering: one run of four keys (slots 5 to 8) forms, and each later key that lands in it pays for the whole run.
Choosing a table size
Because the cost depends on α, tables are resized when α crosses a threshold.
- Chaining implementations often resize near α = 0.75 to 1.
- Open addressing implementations resize earlier, often at α = 0.5 to 0.7, because cost rises steeply near 1.
- Doubling the table when the threshold is crossed makes the total cost of resizing amortized O(1) per insertion.
A question may ask for the largest α that keeps the expected unsuccessful search at or below some number of probes. Solve 1 / (1 − α) ≤ p for α, which gives α ≤ 1 − 1/p. For p = 5, α ≤ 0.8.
Common errors
- Using the chaining formula α for open addressing, or the reverse.
- Forgetting that open addressing needs α < 1, so 1 / (1 − α) is undefined at α = 1.
- Mixing up the successful and unsuccessful cases. Unsuccessful is always the larger.
- Applying the uniform-hashing formulas to linear probing. Use Knuth's formulas for that scheme.
- Counting the hash computation as a probe in one place and not in another. Read the question for whether it asks for comparisons or slots examined.
- Treating deleted slots as empty in open addressing. A deletion must leave a marker, or later searches stop too early. Markers also count as occupied in probe-length estimates.
Exercise
An open addressing table with uniform hashing holds 600 keys in 800 slots. What is the expected number of probes for an unsuccessful search, and for a successful search (use ln 4 ≈ 1.386)?
Show answer
α = 600 / 800 = 0.75. Unsuccessful: 1 / (1 − 0.75) = 4 probes. Successful: (1 / 0.75) · ln(1 / 0.25) = 1.333 · 1.386 ≈ 1.85 probes.
Exercise
A chained hash table has 1000 slots and holds 2500 keys, with simple uniform hashing. What is the expected number of comparisons for an unsuccessful search, and about how many for a successful search?
Show answer
α = 2500 / 1000 = 2.5. Unsuccessful search scans the whole chain, so about 2.5 comparisons. Successful search takes about 1 + α/2 = 1 + 1.25 = 2.25 comparisons.
Exercise
Using the bound 1 / (1 − α), what is the largest load factor for which an unsuccessful search in an open addressing table takes at most 10 expected probes?
Show answer
Solve 1 / (1 − α) ≤ 10. Then 1 − α ≥ 0.1, so α ≤ 0.9. The table may be at most 90% full.