algoritmauzmanı
Sign in
TREN
9.2Open Addressing (Array)
Lesson 9.2.5

Double hashing

With , two keys with the same home cell still follow the same path. gives every key a step size of its own: a second hash function says how many cells the key jumps at a time.

What you will learn
  • How the order (h + i * hash2(k)) % M is computed
  • Why the second function must never return 0
  • Why every cell is reached when the table size is prime
6 minTime O(1) / O(n)Memory O(1)C · Java · Python
Know these first:9.2.4 Quadratic probing

1Idea

The first function gives the home cell: h = k % 11. The second gives the step size: hash2(k) = 7 - k % 7. On attempt i the cell is j = (h + i * hash2(k)) % M. If the home cell is full the key jumps hash2(k) cells, and if that one is full too, it jumps the same distance again.

Keyk % 117 - k % 7Cells tried
25333, 6, 9, 1, ...
36363, 9, 4, 10, ...
47323, 5, 7, 9, ...

All three keys have home cell 3, but their paths split after the first attempt. Keys with the same home cell no longer follow one another: secondary clustering ends too.

Two rules for the second function

  • It must never give 0. With a step of 0, every attempt looks at the home cell and the key never moves. 7 - k % 7 is always between 1 and 7.
  • The step must share no divisor with the table size. If the step and M have no common divisor, M attempts visit every cell once. If M is prime, every step from 1 to M - 1 meets this condition. That is why hash2 is kept below M.

Deletion is again done with a tombstone. Double hashing spreads keys the best of the open addressing methods; the price is two hash computations per key.

2Step by step

The table holds 14 (3), 25 (3 was full, so it jumped 3 cells to 6), 18 (7) and 20 (9). We insert 36: h = 3, hash2(36) = 7 - 1 = 6.

0121434525618782091036key3h6step0i1i2i364j
  1. h = 3, step = 6. i = 0: j = 3, which holds 14.
  2. i = 1: j = 3 + 6 = 9. The cell holds 20.
  3. i = 2: j = (3 + 12) % 11 = 4. The cell is EMPTY.
  4. 36 is written into cell 4. 25, with the same home cell, went to 6: different paths.

#define M 11#define EMPTY -1#define DELETED -2 struct HashTable {    int items[M];    int count;}; int hash(int key) {    return key % M;} int hash2(int key) {    return 7 - key % 7;  // between 1 and 7, never 0} bool insert(struct HashTable* t, int key) {    int h = hash(key);  // home cell    int step = hash2(key);  // jump size    int slot = -1;    for (int i = 0; i < M; i++) {        int j = (h + i * step) % M;  // attempt i        if (t->items[j] == key) {            return false;  // already there        }        if (t->items[j] == DELETED && slot == -1) {            slot = j;        }        if (t->items[j] == EMPTY) {            if (slot == -1) {                slot = j;            }            break;        }    }    if (slot == -1) {        return false;  // the table is full    }    t->items[slot] = key;  // write    t->count++;    return true;} int search(struct HashTable* t, int key) {    int h = hash(key);    int step = hash2(key);    for (int i = 0; i < M; i++) {        int j = (h + i * step) % M;        if (t->items[j] == EMPTY) {            return -1;        }        if (t->items[j] == key) {            return j;        }    }    return -1;}
static final int M = 11;static final int EMPTY = -1;static final int DELETED = -2; class OpenHashTable {    int[] items = new int[M];    int count = 0;     OpenHashTable() {        java.util.Arrays.fill(items, EMPTY);    }     int hash(int key) {        return key % M;    }     int hash2(int key) {        return 7 - key % 7;  // between 1 and 7, never 0    }     boolean insert(int key) {        int h = hash(key);  // home cell        int step = hash2(key);  // jump size        int slot = -1;        for (int i = 0; i < M; i++) {            int j = (h + i * step) % M;  // attempt i            if (items[j] == key) {                return false;  // already there            }            if (items[j] == DELETED && slot == -1) {                slot = j;            }            if (items[j] == EMPTY) {                if (slot == -1) {                    slot = j;                }                break;            }        }        if (slot == -1) {            return false;  // the table is full        }        items[slot] = key;  // write        count++;        return true;    }     int search(int key) {        int h = hash(key);        int step = hash2(key);        for (int i = 0; i < M; i++) {            int j = (h + i * step) % M;            if (items[j] == EMPTY) {                return -1;            }            if (items[j] == key) {                return j;            }        }        return -1;    }}
M = 11EMPTY = -1DELETED = -2  class OpenHashTable:    def __init__(self):        self.items = [EMPTY] * M        self.count = 0     def h(self, key):        return key % M     def h2(self, key):        return 7 - key % 7  # between 1 and 7, never 0     def insert(self, key):        h = self.h(key)  # home cell        step = self.h2(key)  # jump size        slot = -1        for i in range(M):            j = (h + i * step) % M  # attempt i            if self.items[j] == key:                return False  # already there            if self.items[j] == DELETED and slot == -1:                slot = j            if self.items[j] == EMPTY:                if slot == -1:                    slot = j                break        if slot == -1:            return False  # the table is full        self.items[slot] = key  # write        self.count += 1        return True     def search(self, key):        h = self.h(key)        step = self.h2(key)        for i in range(M):            j = (h + i * step) % M            if self.items[j] == EMPTY:                return -1            if self.items[j] == key:                return j        return -1

The snippets focus on the operation in this lesson. The C examples need stdlib.h.

3Complexity

Time
O(1) / O(n)

In a well spread table, attempts on average; in the worst case.

Memory
O(1)

No extra memory is used.

At the same , double hashing makes fewer attempts than linear or quadratic probing, because paths differ by key and no clusters form.

4Common mistake

A common mistake is writing the second function in the same form as the first:

int hash2(int key) {    return key % 7;   // wrong: the step is 0 for 7, 14, 21}

If the key is a multiple of 7, the step is 0; when the home cell is full, the key keeps trying the same cell and cannot be inserted even though the table has room. That is why textbooks use the form R - (k % R) (with R a prime smaller than M).

5Try it

M = 11, hash2(k) = 7 - k % 7. Which cells are tried in the first three attempts for 58?

Show the answer

The answer is B. 58 % 11 = 3, 58 % 7 = 2, so the step is 7 - 2 = 5. The cells are 3, 3 + 5 = 8, (3 + 10) % 11 = 2.

In double hashing, what does a prime table size guarantee?

Show the answer

The answer is B. If M is prime, no step from 1 to M - 1 shares a divisor with M, so M attempts visit each cell once. It does not prevent collisions, and a step of 0 is prevented by the form of the second function.

Lessons you mark show up in the curriculum. If you are signed in, they are saved to your account too.