9.2Open Addressing (Array)
Search
Searching for a key means walking again the path that was followed when it was inserted. If insertion put the key into the first empty cell on the path that starts at its home cell, search looks at the same cells in the same order. Knowing where the path ends is the real topic of this lesson.
- Why search must follow the same
(h + i) % Morder as insertion - Why an
EMPTYcell means "the key is not here" - How many attempts a successful and an unsuccessful search take
1Idea
Search starts with h = key % M and looks at the cells j = (h + i) % M in turn. Each cell is in one of three states: it holds the key being searched for (found, return j), it is EMPTY (the end of the path), or it holds some other key (move on to the next attempt).
Why is it safe to stop at an EMPTY cell? If the key is in the table, it was written into the first empty cell of this path when it was inserted. So the key sits before the first EMPTY cell met on the path. If an EMPTY cell has been reached, the key never got that far: it is not in the table.
If the table is full, there is no EMPTY cell. Then the loop ends after M attempts and returns -1; the whole table has been checked.
In an unsorted array, search looks at every cell. A hash table only looks at the short path that starts at the home cell; when the table is sparse, that path is usually one or two cells.
2Step by step
The table holds 14, 25, 36, 47 and 18; the first four share home cell 3, so a run has formed from 3 to 7. First 36 is searched for, then 58, which is not in the table.
- search(36). The home cell is
h = 36 % 11 = 3. First attemptj = 3: 14. - 14 is neither 36 nor
EMPTY.i = 1,j = 4: 25. Keep going. i = 2,j = 5: 36. Found; the function returns 5. Three attempts were enough.- search(58).
58 % 11 = 3: the search starts at cell 3 again. - Cells 3, 4, 5 and 6 hold other keys.
i = 4,j = 7: 18. Not 58 either. i = 5,j = 8: the cell isEMPTY. If 58 were in the table, it would have appeared before this cell. The function returns -1.
#define M 11#define EMPTY -1 struct HashTable { int items[M]; int count;}; int hash(int key) { return key % M;} int search(struct HashTable* t, int key) { int h = hash(key); // home cell for (int i = 0; i < M; i++) { int j = (h + i) % M; // attempt i if (t->items[j] == EMPTY) { return -1; // end of the path: not in the table } if (t->items[j] == key) { return j; // found } } return -1; // the whole table was walked}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; } int search(int key) { int h = hash(key); // home cell for (int i = 0; i < M; i++) { int j = (h + i) % M; // attempt i if (items[j] == EMPTY) { return -1; // end of the path: not in the table } if (items[j] == key) { return j; // found } } return -1; // the whole table was walked }}M = 11EMPTY = -1 class OpenHashTable: def __init__(self): self.items = [EMPTY] * M self.count = 0 def h(self, key): return key % M def search(self, key): h = self.h(key) # home cell for i in range(M): j = (h + i) % M # attempt i if self.items[j] == EMPTY: return -1 # end of the path: not in the table if self.items[j] == key: return j # found return -1 # the whole table was walkedThe snippets focus on the operation in this lesson. The C examples need stdlib.h.
3Complexity
When the table is sparse, one or two cells after the home cell are checked: O(1) on average. As runs grow the path gets longer; in the worst case the whole table is checked: O(n).
A few variables are enough.
An unsuccessful search costs more than a successful one, because for a missing key the path only ends at an EMPTY cell. The same run made the search for 58 walk from 3 to 8. The gap widens as the load factor goes up.
4Common mistake
A common mistake is looking only at the home cell:
int search(struct HashTable* t, int key) { int h = hash(key); return t->items[h] == key ? h : -1; // wrong: misses keys that slid}This function finds 14 but not 36, because 36 is not in its home cell but two cells further on. With open addressing, the place of a key is found only by walking again the path that insertion followed.
5Try it
In the table of the drawing (3: 14, 4: 25, 5: 36, 6: 47, 7: 18), how many cells does search(25) look at?
The answer is B. 25 % 11 = 3. Cell 3 holds 14; at j = 4 25 is found. Two cells are checked.
Show the answer
The answer is B. 25 % 11 = 3. Cell 3 holds 14; at j = 4 25 is found. Two cells are checked.
In the same table, what does search(40) return?
The answer is B. 40 % 11 = 7. Cell 7 holds 18, and j = 8 is EMPTY. The search returns -1 on the second attempt. The home cell is at the end of the run, so the path is short.
Show the answer
The answer is B. 40 % 11 = 7. Cell 7 holds 18, and j = 8 is EMPTY. The search returns -1 on the second attempt. The home cell is at the end of the run, so the path is short.
Lessons you mark show up in the curriculum. If you are signed in, they are saved to your account too.