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

Quadratic probing

With , filled cells pile up side by side, and as a run grows every new key walks to its end. sends a colliding key not to the next cell but to cells further and further away: 1, 4, 9, 16 steps on.

What you will learn
  • Which cells the order (h + i*i) % M tries
  • Why primary clustering drops, and what secondary clustering is
  • Why quadratic probing cannot reach every cell, and the load factor limit
6 minTime O(1) / O(n)Memory O(1)C · Java · Python

1Idea

The insert and search code is the same as with linear probing; only the next cell is computed differently: j = (h + i * i) % M. For a key with home cell 3, the cells tried are 3, 4, 7, 12 % 11 = 1, 19 % 11 = 8, and so on.

With linear probing, every key that lands in a run is added at its end and makes it one longer. This is called primary clustering. With quadratic probing the steps grow, so a key leaves a run quickly; keys from neighbouring home cells do not share the same path.

Still, two keys with the same home cell try the same cells in the same order. This is called secondary clustering; it is milder but real. Double hashing removes it too.

It cannot reach every cell

The values of i * i % 11 repeat as 0, 1, 4, 9, 5, 3, 3, 5, 9, 4, 1: in an 11-cell table a key tries at most 6 different cells. Even if the table has room, an insertion can fail. The rule is: if the table size is prime and the table is at most half full ( at most 0.5), quadratic probing always finds an empty cell.

i012345
i * i01491625
(3 + i * i) % 11347186

Deletion works the same way, with a tombstone; the code changes only in the probing order.

2Step by step

The table holds 14 (3), 25 (4) and 18 (7). We insert 36, whose home cell is 36 % 11 = 3. Linear probing would put 36 in cell 5, at the end of the run.

01214325456187891036key3h0i1i2i3i361j
  1. h = 3, i = 0: j = 3. The cell holds 14.
  2. i = 1: j = 3 + 1 = 4. The cell holds 25.
  3. i = 2: j = 3 + 4 = 7. A jump of 4 cells; this one holds 18.
  4. i = 3: j = (3 + 9) % 11 = 1. It wraps to the start of the table, and the cell is EMPTY.
  5. 36 is written into cell 1. The run at 3 and 4 did not grow.

#define M 11#define EMPTY -1#define DELETED -2 struct HashTable {    int items[M];    int count;}; int hash(int key) {    return key % M;} bool insert(struct HashTable* t, int key) {    int h = hash(key);  // home cell    int slot = -1;    for (int i = 0; i < M; i++) {        int j = (h + i * i) % M;  // quadratic step        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;  // no free cell found    }    t->items[slot] = key;  // write    t->count++;    return true;} int search(struct HashTable* t, int key) {    int h = hash(key);    for (int i = 0; i < M; i++) {        int j = (h + i * i) % 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;    }     boolean insert(int key) {        int h = hash(key);  // home cell        int slot = -1;        for (int i = 0; i < M; i++) {            int j = (h + i * i) % M;  // quadratic step            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;  // no free cell found        }        items[slot] = key;  // write        count++;        return true;    }     int search(int key) {        int h = hash(key);        for (int i = 0; i < M; i++) {            int j = (h + i * i) % 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 insert(self, key):        h = self.h(key)  # home cell        slot = -1        for i in range(M):            j = (h + i * i) % M  # quadratic step            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  # no free cell found        self.items[slot] = key  # write        self.count += 1        return True     def search(self, key):        h = self.h(key)        for i in range(M):            j = (h + i * i) % 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)

While the load factor is under 0.5, attempts on average. In the worst case every attempt is made: .

Memory
O(1)

No extra memory is used.

With quadratic probing i * i grows; to avoid overflow in very large tables, the next cell can also be found by adding 2i - 1 to the previous one, because consecutive squares differ by odd numbers.

4Common mistake

A common mistake is adding the quadratic step to the previous cell tried:

j = (j + i * i) % M;   // wrong: the steps add up, the order is not h + i²

This code tries cells 3, 4, 8, 17 % 11 = 6, and so on; as long as insert and search use the same wrong order it seems to work, but it is not the order exams ask for, and there is no guarantee about which cells it reaches. Every attempt is computed from the home cell: (h + i * i) % M.

5Try it

With M = 11 and quadratic probing, which cells are tried in the first four attempts for a key whose home cell is 5?

Show the answer

The answer is B. 5 + 0 = 5, 5 + 1 = 6, 5 + 4 = 9, (5 + 9) % 11 = 3. 14 is outside the table; % M makes it 3.

With quadratic probing, what can happen when the table is more than half full?

Show the answer

The answer is B. In an 11-cell table a key tries only 6 different cells. If all of them are full, insertion finds no place even though other cells of the table are empty.

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