ENع
→Load factor and resizing
الدرس 11 · Hashing and heaps

Load factor, resizing and universal hashing

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

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

A hash table is only fast while its chains stay short. Watch keys stack up in the buckets of a small Python table, measure how full it is with the load factor α = n/m, and see the table double and rehash every key when it fills up, at an average cost of O(1) per insert. Then an attacker piles five keys into one bucket, and a hash function picked at random from a universal family spreads them out again. The Software lesson "Hash maps: finding things by key" shows buckets and a table growing; "Chaining vs probing: expected cost and clustering" covers what happens inside a bucket, and "Amortised analysis: the accounting and potential methods" proves the doubling bound in full.

ستتعلّم

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

شكل 11 · تفاعلي
01 / 08

Keys go into chains

A table of m = 4 buckets, each holding a list called a chain. put sends key k to bucket h(k) % m. For small whole numbers, Python's hash(k) is k itself. Put 5, 12, 9 and 2: 5 and 9 both pick bucket 1.

h(k) = hash(k) = k   (hash(5) == 5 in Python)
put  5:  5 % 4 = 1
put 12:  12 % 4 = 0
put  9:  9 % 4 = 1
put  2:  2 % 4 = 2

  bucket 0: [12]
  bucket 1: [5, 9]
  bucket 2: [2]
  bucket 3: []
جارٍ تحميل الرسم…
أو ← →
اقرأ الدرس خطوة بخطوة
  1. Keys go into chains

    A table of m = 4 buckets, each holding a list called a chain. put sends key k to bucket h(k) % m. For small whole numbers, Python's hash(k) is k itself. Put 5, 12, 9 and 2: 5 and 9 both pick bucket 1.

    h(k) = hash(k) = k   (hash(5) == 5 in Python)
    put  5:  5 % 4 = 1
    put 12:  12 % 4 = 0
    put  9:  9 % 4 = 1
    put  2:  2 % 4 = 2
    
      bucket 0: [12]
      bucket 1: [5, 9]
      bucket 2: [2]
      bucket 3: []
  2. Load factor: keys per bucket

    The load factor α = n/m counts keys per bucket: here 4/4 = 1. It is the average chain length. If keys spread evenly, a search walks about 1 + α entries, so a small α keeps lookups fast.

    α = n / m = 4 / 4 = 1
    chain lengths: 1, 2, 1, 0   average 4/4 = 1
    
    chaining, keys spread evenly (CLRS §11.2):
      average search cost Θ(1 + α)
      α ≤ 1 → about 2 entries per search at most
  3. Full: time to grow

    Now put 7. Line 2 checks first: n == m, so every bucket already holds one key on average. One more key would push α above 1, so put calls grow before 7 goes in.

    put 7:
      t.n == t.m  →  4 == 4  →  True
      so grow(t) runs first
    without it: α = 5/4 > 1, and it keeps rising
  4. Double and rehash

    grow lifts every key out into a list, doubles m to 8, then puts each key back at h(k) % 8. 12 moves to bucket 4 and 5 to bucket 5; 9 and 2 stay. Then 7 lands in bucket 7: α = 5/8.

    old = [12, 5, 9, 2]   (chains read in bucket order)
    t.m = 2 * 4 = 8
      12 % 8 = 4   (was 0)
       5 % 8 = 5   (was 1)
       9 % 8 = 1   (was 1)
       2 % 8 = 2   (was 2)
    put  7:  7 % 8 = 7
    
      bucket 0: []
      bucket 1: [9]
      bucket 2: [2]
      bucket 3: []
      bucket 4: [12]
      bucket 5: [5]
      bucket 6: []
      bucket 7: [7]
    α = 5/8
  5. Rare big costs average out

    A grow touches all n keys, so it costs O(n). But doubling means the next grow waits until n more keys arrive. Over N puts, grows move fewer than 2N keys in total: O(1) per put on average, or amortised.

    start m = 4; grow when n == m, moving n keys
       puts N   keys moved   2N
            5            4    10
            9           12    18
           17           28    34
           33           60    66
    moves = 4 + 8 + ... + m_last < 2·m_last ≤ 2N
    growing by +4 each time instead: Θ(N²) moves
  6. A fixed hash can be beaten

    Anyone who knows h(k) = k can pick keys that leave the same remainder: 3, 11, 19, 27 and 35 all give k % 8 = 3. One chain of 5: a search walks every key, though α is only 5/8.

    new table, m = 8, h(k) = k
       3 % 8 = 3
      11 % 8 = 3
      19 % 8 = 3
      27 % 8 = 3
      35 % 8 = 3
    
    bucket 3: [3, 11, 19, 27, 35]
    search cost: Θ(n), not Θ(1 + α)
  7. Pick the hash at random

    Pick h at random when the table is made: h(k) = ((a·k + b) % 37) % m, with a prime 37 above every key and random a, b below it (a ≠ 0). This draw, a = 4, b = 9, spreads the keys.

    draw: a = 4, b = 9, p = 37
       3: (4·3 + 9) % 37 = 21,  % 8 = 5
      11: (4·11 + 9) % 37 = 16,  % 8 = 0
      19: (4·19 + 9) % 37 = 11,  % 8 = 3
      27: (4·27 + 9) % 37 =  6,  % 8 = 6
      35: (4·35 + 9) % 37 =  1,  % 8 = 1
    
      bucket 0: [11]
      bucket 1: [35]
      bucket 2: []
      bucket 3: [19]
      bucket 4: []
      bucket 5: [3]
      bucket 6: [27]
      bucket 7: []
  8. Universal: collisions stay rare

    The family is universal: two different keys share a bucket in at most 1/m of the draws. The worst pair here collides in 136 of 1332. So a key's chain holds at most 1 + (n − 1)/m = 1.5 keys on average.

    draws (a, b): 36 × 37 = 1332
    any pair x ≠ y in 0..36: collide in ≤ 1332/8 = 166.5 draws
      worst pair, counted: 136
    expected chain of a key ≤ 1 + (n − 1)/m = 1 + 4/8 = 1.5
      these 5 keys, all 1332 draws: 1.41
    the attacker cannot see a and b

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