9.2Open Addressing (Array)
Insert with linear probing
When two keys land on the same cell, where does the second one go? Open addressing looks for the answer inside the table: if the key's home cell is full, try the one next to it, and if that is full too, the one after. In this lesson we code the simplest order, linear probing.
- How the next cells are tried with
(h + i) % Mwhen the home cell is full - How the search wraps from the end of the table to its start, and how a full table is detected
- Why linear probing causes clustering
1Idea
The table is an array of M = 11 cells. An empty cell holds EMPTY (-1); keys are never negative, so this value cannot be mistaken for a key. The first cell tried for a key is its home cell: h = key % M.
If the home cell is empty, the key is written there and the job is done. If it is full, the cells h + 1, h + 2, ... are tried in turn. On attempt i the cell is j = (h + i) % M. Thanks to % M, after the last cell the search goes back to cell 0; the table is walked like a ring.
(9 + 2) % 11 = 0: the key wraps around to the start of the table.Before inserting, the function also checks whether the key is already there, because a key is kept only once. No separate search is needed: if the key is in the table, it lies on this path before the first empty cell. If the key itself turns up on the way, the insertion returns false.
If the table is full (count == M), no cell is empty, so the function returns false without trying. The loop also makes at most M attempts, so it can never run forever.
There is no deletion in this lesson. Once deletion arrives (the delete lesson), stopping at the first empty cell will no longer be enough, and insertion will change a little.
2Step by step
The table holds 14 (cell 3), 25 (home cell 3, which was full, so it sits in cell 4) and 18 (cell 7). We insert 36: 36 % 11 = 3.
- Start. The table holds 3 keys and
countis 3. The table is not full. - The home cell is
h = 36 % 11 = 3. First attempt:i = 0,j = 3. - Cell 3 holds 14: neither 36 nor
EMPTY. On to the next attempt. i = 1,j = 4. This cell holds 25; it slid here because its home cell is also 3.i = 2,j = 5. The cell isEMPTY: 36 is not in the table, and a place has been found.- 36 is written into cell 5 and
countbecomes 4. Cells 3, 4 and 5 now form one run of filled cells.
#define M 11#define EMPTY -1 struct HashTable { int items[M]; int count;}; void create(struct HashTable* t) { for (int i = 0; i < M; i++) { t->items[i] = EMPTY; } t->count = 0;} int hash(int key) { return key % M;} bool insert(struct HashTable* t, int key) { if (t->count == M) { return false; // the table is full } int h = hash(key); // home cell for (int i = 0; i < M; i++) { int j = (h + i) % M; // attempt i if (t->items[j] == key) { return false; // already there } if (t->items[j] == EMPTY) { t->items[j] = key; // the first empty cell t->count++; return true; } } return false;}static final int M = 11;static final int EMPTY = -1; 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) { if (count == M) { return false; // the table is full } int h = hash(key); // home cell for (int i = 0; i < M; i++) { int j = (h + i) % M; // attempt i if (items[j] == key) { return false; // already there } if (items[j] == EMPTY) { items[j] = key; // the first empty cell count++; return true; } } return false; }}M = 11EMPTY = -1 class OpenHashTable: def __init__(self): self.items = [EMPTY] * M self.count = 0 def h(self, key): return key % M def insert(self, key): if self.count == M: return False # the table is full h = self.h(key) # home cell for i in range(M): j = (h + i) % M # attempt i if self.items[j] == key: return False # already there if self.items[j] == EMPTY: self.items[j] = key # the first empty cell self.count += 1 return True return FalseThe snippets focus on the operation in this lesson. The C examples need stdlib.h.
3Complexity
When the table is sparse, the home cell is usually empty and an insertion takes O(1) attempts on average. As the table fills up the attempts get longer; in the worst case every cell is tried: O(n).
Only a few variables are used; the key is written into the array itself.
The average number of attempts depends on how full the table is, that is on the load factor. With linear probing, clustering grows quickly once the load factor passes 0.5.
4Common mistake
A common mistake is computing the next cell without % M:
int j = h + i; // wrong: after 10 come 11, 12 ... outside the arrayFor a key whose home cell is 9, if cells 9 and 10 are full, j becomes 11 and the write lands outside the array. C allows it and memory is corrupted; Java and Python throw an error. The right form is (h + i) % M: the table is a ring, and cell 0 comes after cell 10.
5Try it
If 47 is inserted into the last table of the drawing (3: 14, 4: 25, 5: 36, 7: 18), which cell does it go into?
The answer is B. 47 % 11 = 3. Cells 3, 4 and 5 are full; on attempt i = 3, j = 6 is empty. 47 goes into cell 6 and the run now stretches from 3 to 7. This is how clustering grows.
Show the answer
The answer is B. 47 % 11 = 3. Cells 3, 4 and 5 are full; on attempt i = 3, j = 6 is empty. 47 goes into cell 6 and the run now stretches from 3 to 7. This is how clustering grows.
Why does insert return false when it meets the key itself on the way?
The answer is B. A hash table keeps each key once. If the key is in the table, it sits on the path from its home cell before the first empty cell; the insertion sees it there and returns false without changing the table.
Show the answer
The answer is B. A hash table keeps each key once. If the key is in the table, it sits on the path from its home cell before the first empty cell; the insertion sees it there and returns false without changing the table.
Lessons you mark show up in the curriculum. If you are signed in, they are saved to your account too.