ENع
→Chaining vs probing
الدرس 10 · Hashing and heaps

Chaining vs probing: expected cost and clustering

لم يُترجم بعد؛ يُعرض باللغة الإنجليزية

14 دقيقة9 خطوات5 أسئلةIntermediate
عن هذا الدرس

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.

ستتعلّم

حدّد ما تشعر بأنك أتقنته. هذا التقييم الذاتي لا يؤثر في درجة الدرس.

شكل 10 · تفاعلي
01 / 09

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)
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. 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)
  2. 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 2
  3. A 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)
  4. 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-4
  5. Five 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, 1
  6. Clusters 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)
  7. 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.8
  8. Cost 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 = α
  9. 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

اختبار يتاح بعد الخطوة الأخيرة.