Chaining vs probing: expected cost and clustering
لم يُترجم بعد؛ يُعرض باللغة الإنجليزية
عن هذا الدرس
Two keys that hash to the same slot collide, and a hash table needs a rule for that. Insert the same five keys into two 7-slot tables: separate chaining keeps a small list in each slot, linear probing walks right to the next free slot. Count every compare and probe, watch full slots join into a cluster, and see why probing gets slow as the table fills while chaining stays close to the load factor. The Software lesson "Hash maps: finding things by key" introduces buckets, collisions and a table that grows.
One key, two tables
Both tables have 7 slots and pick a slot the same way: i = hash(k) % 7. For a small whole number, hash(k) is k itself. So key 15 goes to slot 1, because 15 % 7 = 1. No clash yet.
m = 7 slots, h(k) = hash(k) % 7 hash(15) = 15, 15 % 7 = 1 chaining: slot 1 list = [15] probing: slot 1 = 15 (1 probe)
اقرأ الدرس خطوة بخطوة
One key, two tables
Both tables have 7 slots and pick a slot the same way: i = hash(k) % 7. For a small whole number, hash(k) is k itself. So key 15 goes to slot 1, because 15 % 7 = 1. No clash yet.
m = 7 slots, h(k) = hash(k) % 7 hash(15) = 15, 15 % 7 = 1 chaining: slot 1 list = [15] probing: slot 1 = 15 (1 probe)A collision
22 % 7 = 1 as well: two keys want slot 1. That is a collision. Chaining simply appends 22 to slot 1's list. Probing finds slot 1 taken, steps right to slot 2 and stores 22 there: 2 probes.
22 % 7 = 1 (same slot as 15) chaining: slot 1 list = [15, 22] probing: slot 1 full → slot 2 empty 2 probes, 22 lands in slot 2A run forms
3 % 7 = 3, and slot 3 is free in both tables: 1 probe. But in the probing table, slots 1, 2 and 3 are now full side by side. A row of full slots like this is called a cluster.
3 % 7 = 3, free in both tables probing table now: slot 0 1 2 3 4 5 6 . 15 22 3 . . . cluster: slots 1-3 (3 in a row)Walking the cluster
8 % 7 = 1 again. Chaining adds a third key to slot 1's list. Probing must walk the whole cluster: slots 1, 2 and 3 are full, so 8 lands in slot 4 after 4 probes. The cluster grew.
8 % 7 = 1 chaining: slot 1 list = [15, 22, 8] probing: 1 full, 2 full, 3 full, 4 empty 4 probes, 8 lands in slot 4 cluster: slots 1-4Five keys in
5 % 7 = 5: free in both tables, 1 probe. All five keys are in. Chaining has lists of 3, 1 and 1. Probing has one cluster, slots 1 to 5, with only slots 0 and 6 still empty.
5 % 7 = 5, free in both tables chaining lists: 0:[] 1:[15,22,8] 2:[] 3:[3] 4:[] 5:[5] 6:[] probing slots: . 15 22 3 8 5 . probes per insert: 1, 2, 1, 4, 1Clusters attract keys
A new key with hash 1, 2, 3, 4, 5 or 6 ends up in slot 6: try 9, which walks from slot 2. That is 6 chances in 7. Slot 0 catches only hash 0. Long clusters grow fastest: primary clustering.
where would a new key land? hash % 7 = 0 → slot 0 hash % 7 = 1 → slot 6 hash % 7 = 2 → slot 6 hash % 7 = 3 → slot 6 hash % 7 = 4 → slot 6 hash % 7 = 5 → slot 6 hash % 7 = 6 → slot 6 9 % 7 = 2: probes 2, 3, 4, 5, 6 (5 probes)Cost of finding a key
Finding a stored key repeats its insert. Chaining compares 1, 2, 1, 3, 1 times: 8/5 = 1.6 on average. Probing takes 1, 2, 1, 4, 1 probes: 9/5 = 1.8. Finding 8 walks slots 1 to 4 again.
search hit = same work as the insert key chaining probing 15 1 1 22 2 2 3 1 1 8 3 4 5 1 1 avg 1.6 1.8Cost of a miss
Search for 29, which is missing (29 % 7 = 1). Chaining checks slot 1's 3 keys and stops. Probing walks to an empty slot: 6 probes. Over all 7 start slots: 5/7 ≈ 0.71 compares against 22/7 ≈ 3.14 probes.
search miss, by start slot s s chaining probing 0 0 1 1 3 6 2 0 5 3 1 4 4 0 3 5 1 2 6 0 1 avg 0.71 3.14 chaining average = n/m = αThe load factor decides
Load factor α = keys ÷ slots, here 5/7. A chaining miss costs about α compares. A linear-probing miss costs about ½(1 + 1/(1 − α)²) probes: 2.5 at α = ½ but 50.5 at α = 0.9. So probing tables resize before half full.
α = n/m = 5/7 ≈ 0.71 expected cost of a miss (large tables, uniform hashing) α chaining linear probing 0.25 0.25 1.4 0.50 0.50 2.5 0.75 0.75 8.5 0.90 0.90 50.5 hit: ½(1 + 1/(1 − α)) probes, 5.5 at α = 0.9 algs4 LinearProbingHashST doubles at n ≥ m/2
أسئلة سريعة: 5
اختر إجابة واحدة لكل سؤال. يمكنك تغيير اختياراتك قبل التحقق منها.
01A linear-probing table with 7 slots holds keys in slots 2, 3 and 4 only. Key 16 arrives and 16 % 7 = 2. How many probes does its insert take, and where does it land?
Slots 2, 3 and 4 are full, so the insert checks 2, 3, 4 and then 5, which is empty: 4 probes, and 16 lands in slot 5. The cluster 2–4 grows to 2–5.
02In the lesson's chained table, slot 1 holds the list [15, 22, 8]. How many key compares does a search for the missing key 29 make (29 % 7 = 1)?
A miss in chaining compares the key with every key in its slot's list, then stops: 3 compares. Only the one list is searched; the other slots are never touched.
03What is primary clustering?
With linear probing, any key that hashes anywhere into a run of full slots ends up just past the run. The empty slot just after a run of i full slots is filled by i + 1 different hash values, so long runs grow fastest.
04Under uniform hashing, a linear-probing miss costs about ½(1 + 1/(1 − α)²) probes. Roughly how many probes is that at α = 0.9?
1 − α = 0.1, so 1/(1 − α)² = 100 and ½(1 + 100) = 50.5. At α = ½ the same formula gives only 2.5. That is why probing tables resize before they are half full.
05Which statement about load factor α = n/m is true?
Open addressing stores one key per slot, so n can never exceed m. Chained lists can hold any number of keys; a chaining miss costs about α compares, so α still matters, it just grows slowly.
المسرد
- Collision
- Two different keys whose hash gives the same slot.
- Separate chaining
- Each slot holds a list of every key that hashes there. A collision just makes that list longer.
- Open addressing
- Every key lives in the table's own slots, one per slot. On a collision the table tries other slots in a fixed order.
- Linear probing
- Open addressing that tries the next slot, then the next: i, i + 1, i + 2, … wrapping round at the end.
- Probe
- One look at a slot while searching or inserting.
- Cluster
- A run of full slots next to each other in an open-addressing table.
- Primary clustering
- The way linear-probing clusters merge and grow, because any key that lands in a cluster is pushed to its end.
- Load factor (α)
- Number of keys divided by number of slots, n/m. It sets the expected cost of both methods.
الملاحظات والمصادر
- The averages 1/2(1 + 1/(1 − α)) for a hit and 1/2(1 + 1/(1 − α)²) for a miss are Sedgewick and Wayne's Proposition M for linear probing in a large table, assuming the hash spreads keys evenly and independently (uniform hashing). Chaining's miss cost of about α compares, Θ(1 + α) with the hashing, is CLRS chapter 11. A 7-slot table is far too small for these limits, so the figure counts its own exact numbers instead.
- MIT 6.006 calls a cluster a "consecutive group of occupied slots" and notes that the longer a cluster gets, the more likely it is to grow. Better probe sequences such as double hashing avoid this primary clustering.
- Real tables resize to keep α small. The algs4 linear-probing table doubles when it is half full and halves when it is one eighth full; MIT 6.006 says open addressing degrades past about 70% full. This 5/7 table is already past that.
- The code is real Python 3: chaining uses t = [[] for _ in range(7)], probing uses t = [None] * 7. The probing put assumes at least one slot is empty, or the while loop would never end.
- For a non-negative whole number n, Python's hash(n) is n % (2**61 − 1) on 64-bit builds, so it equals n for these small keys (Python docs, Hashing of numeric types).
- Sedgewick and Wayne, Algorithms 4th ed., §3.4 Hash Tables
- Sedgewick and Wayne, LinearProbingHashST.java (resizes at half full)
- MIT 6.006 Introduction to Algorithms (Spring 2020), Lecture 4: Hashing (notes, PDF)
- MIT 6.006 Introduction to Algorithms (Fall 2011), Lecture 10: Open addressing (notes, PDF)
- Cormen, Leiserson, Rivest and Stein, Introduction to Algorithms, 4th ed. (MIT Press, 2022), chapter 11
- Linear probing (overview and analysis history)
- Python Standard Library: Hashing of numeric types
- Python built-in functions: hash()
اختبار يتاح بعد الخطوة الأخيرة.