การแฮช (Hashing)
แนะนำใหม่
7:45
โครงสร้างข้อมูลและอัลกอริทึม
This course is about choosing a container by what it costs. The first unit is the yardstick: how cost grows with the size of the input. The next four are the…
บรรยายภาษาอังกฤษ · สลับคำบรรยายภาษาอังกฤษและภาษาจีนได้
ในบทเรียนนี้
| English | ความหมาย |
|---|---|
| hash function/hæʃ ˈfʌŋkʃn/ | ฟังก์ชันแฮช |
| collision/kəˈlɪʒn/ | การชนกัน |
| Chaining/ˈtʃeɪnɪŋ/ | a hash-table method that keeps all keys with the same hash in a list at that slot |
| Open addressing/ˈəʊpən əˈdresɪŋ/ | a hash-table method that stores every key in the array itself and probes other slots when one is taken |
| load factor/ləʊd ˈfæktə/ | ตัวชี้โหลด |
| Clustering/ˈklʌstərɪŋ/ | the build-up of long runs of occupied slots in a hash table, which makes probes longer |
| resizes/rɪˈsaɪzɪz/ | grows the table (usually doubling it) and re-inserts every key when it becomes too full |
| Universal hashing/ˌjuːnɪˈvɜːsl ˈhæʃɪŋ/ | Choosing a hash function at random from a family so that any two keys collide with probability at most one over the number of buckets. |
Spread, collide, resolve
- A hash function 哈希函数 maps a key to a bucket. $h(k) = k \bmod 13$ sends $4, 17, 30, 43$ all to bucket $4$: a collision 冲突.
- Chaining 链地址法 keeps a list in each bucket. Open addressing 开放寻址法 walks on to the next free slot.

Under h(k) = k mod 13, the keys 4, 17, 30, 43 all land —
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Each key leaves remainder 4 on division by 13 — one crowded bucket.
The load factor rules the chain
- With $m$ buckets and $n$ keys, the load factor 负载因子 $\alpha = n/m$ is also the average chain length. Our keys $[4, 17, 30, 43, 12, 25, 38]$ give $\alpha = 7/13 \approx 0.54$: bucket $4$ holds four keys, bucket $12$ holds three, and the other eleven buckets are empty.
- With chaining, a successful search costs about $1 + \alpha/2$ comparisons. Grow $m$ as $n$ grows and the expected cost stays $O(1)$. That resizing is why dictionaries stay fast.
Seven keys in thirteen buckets give a load factor of —
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Alpha is keys over buckets: 7/13 is about 0.54 — also the average chain length.
Probing and clustering
- Linear probing on $4, 17, 30, 43$, which all hash to slot 4: each key tries the next slot until it finds an empty one.
| Key | Slots tried | Ends in | Probes |
|---|---|---|---|
| 4 | 4 | 4 | 1 |
| 17 | 4, 5 | 5 | 2 |
| 30 | 4, 5, 6 | 6 | 3 |
| 43 | 4, 5, 6, 7 | 7 | 4 |
- In total $1 + 2 + 3 + 4 = 10$ probes for four inserts: each new key walks the whole cluster.
- Clustering 聚集 makes those runs longer, so real tables keep $\alpha$ under about $0.75$. Do not take the key mod a power of two when keys share low bits (pick a prime), and never change a key while it is in the table.
Linear-probing inserts of 4, 17, 30, 43 take —
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
1 + 2 + 3 + 4 = 10: each new key walks the growing cluster.
When the table fills up
- Open addressing pays for a high load factor. With linear probing, the expected cost of a miss is about $\tfrac12\left(1 + \dfrac{1}{(1-\alpha)^2}\right)$: $2.5$ probes at $\alpha = 0.5$, but $8.5$ at $\alpha = 0.75$ and $50.5$ at $\alpha = 0.9$.
- So the table resizes 扩容: it roughly doubles $m$ (to a prime such as $29$ after $13$) and reinserts every key, because $h(k) = k \bmod m$ changes when $m$ changes. One resize costs $O(n)$, but doubling makes it amortised $O(1)$ per insert, like a growing array.
Using the linear-probing estimate for a miss, how many probes at load factor 0.8?
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
One half of (1 + 1/0.2^2) = one half of (1 + 25) = 13 probes.
After a resize, every key can stay in its old bucket number.
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
The bucket is k mod m, and m has changed, so every key must be rehashed.
Collisions come early: the birthday bound
- With $m$ buckets, a collision becomes more likely than not after about $1.18\sqrt m$ keys: 23 people for 365 birthdays.
- In the 13-bucket table, 4 keys all miss each other with probability $13 \cdot 12 \cdot 11 \cdot 10/13^4 = 0.60$, and 5 keys with only $0.42$. Collisions are normal, so the table must handle them well.
Probability that 5 keys land in 5 different buckets of 13 (two decimals)?
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
$13 \cdot 12 \cdot 11 \cdot 10 \cdot 9/13^5 = 0.416$.
With 13 buckets, collisions are unlikely until the table is nearly full.
ความยาก 1/5 · 100 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
Five keys already collide with probability 0.58.
Worked example: universal hashing
- An attacker who knows $h(k) = k \bmod 13$ can send only keys that leave remainder 4, and every search becomes $O(n)$. Universal hashing 通用哈希 picks $h(k) = ((ak + b) \bmod p) \bmod m$ at random when the table is made.
- With $a = 7$, $b = 5$, $p = 101$: keys 4, 17, 30, 43 go to buckets 7, 10, 0 and 3. For distinct keys in $\{0,\ldots,p-1\}$, choosing $a$ uniformly from $\{1,\ldots,p-1\}$ and $b$ uniformly from $\{0,\ldots,p-1\}$ gives collision probability at most $1/m$.
$h(k) = ((7k + 5) \bmod 101) \bmod 13$: bucket of key 17?
ความยาก 2/5 · 140 เหรียญ + บอนัสน้อยๆ เมื่อทำสำเร็จครั้งแรกในวันนี้
$7 \times 17 + 5 = 124$; $124 \bmod 101 = 23$; $23 \bmod 13 = 10$.
Marks that slip away
- A power of two with patterned keys. $k \bmod 16$ keeps only the low 4 bits, so all keys that share them collide. Use a prime $m$.
- Letting the load factor climb. Linear probing at $\alpha = 0.75$ already costs about $8.5$ probes per miss. Resize before that.
- Changing a key in the table. Its bucket came from the old value, so the table can no longer find it.
Recap
- Load factor $\alpha = n/m$ is the average chain length; with chaining a hit costs about $1 + \alpha/2$.
- Linear probing forms clusters: keep $\alpha$ under about $0.75$, then resize and rehash.
- Use a prime modulus, and never change a key that is in the table.
เกิดปัญหาอะไรขึ้น? เฉพาะทีมเว็บไซต์เท่านั้นที่เห็นข้อมูลนี้