AQA 7517, section 4.2.6.1
A hash table is a data structure that creates a mapping between keys and values. Instead of searching through a list for an item, a hash table works out where the item should be directly from its key, so lookup is very fast.
The hash function
A hash function takes a key and turns it into a number, which is the position (index) in the table where that item is stored.
A simple hash function for whole-number keys is the remainder after dividing by the table size:
def hash_key(key, size):
return key % size
print(hash_key(47, 10)) # 7
print(hash_key(23, 10)) # 3
With a table of 10 slots, key 47 goes in slot 7 and key 23 goes in slot 3.
For text keys, a common approach is to add up the character codes of the letters and then take the remainder:
def hash_text(word, size):
total = 0
for ch in word:
total = total + ord(ch)
return total % size
print(hash_text("cat", 10)) # 99 + 97 + 116 = 312, so 2
A good hash function gives results that are spread evenly across the table, is quick to calculate, and always gives the same result for the same key.
Storing and finding items
To store a key, work out its position with the hash function and place the item there. To find or delete it, work out the same position and go straight to it. No searching is needed, so on average the time taken doesn't depend on how many items are stored.
Here the key is a name, and each record holds a name and a phone number. The hash function adds up the character codes of the letters (for Priya, 80 + 114 + 105 + 121 + 97 = 517) and takes the remainder when divided by the table size, 8.
Collisions
A collision happens when two different keys produce the same hash value, so they should go in the same slot. For example, in the table above, Priya and Ben both give 5 (517 MOD 8 = 5 and 277 MOD 8 = 5).
Handling collisions by rehashing
Rehashing means that when the position is already taken, a rule is used to find another position. The simplest rule is to move to the next free slot, wrapping round to the start if the end of the table is reached. This is often called linear probing.
To find an item, you go to its hashed position. If a different item is there, you follow the same rule, checking one slot after another until you find it, or reach an empty slot, which means it isn't in the table.
Uses
- Dictionaries and sets in programming languages.
- Databases and indexes, for quick access to records.
- Caches, which keep recent results for fast reuse.
- Symbol tables in compilers, which look up variable names.
- Checking passwords, using a hash of the password as the stored value.
Worked example
Task: a hash table has 7 slots (0 to 6), and the hash function is key % 7. Insert the keys 15, 22, 8 and 1 in that order, using rehashing to the next free slot. Show the final table.
- 15: , so slot 1 is free. Store it there.
- 22: , but slot 1 is taken (a collision). Try slot 2, which is free. Store it there.
- 8: , taken. Slot 2 is taken. Slot 3 is free. Store it there.
- 1: , taken. Slots 2 and 3 are taken. Slot 4 is free. Store it there.
To find 8 later, you hash to slot 1, see 15, move to slot 2, see 22, then move to slot 3 and find 8. The more collisions there are, the slower this gets, which is why a good hash function and a table that isn't too full matter.
Exam tip: in a hash table question, show the calculation for every key, such as ", collision, so try slot 2". Then say which slot it ends up in.
Check yourself
What is the purpose of a hash function?
To turn a key into a position in the table, so the item can be stored and found directly without searching.
What is a collision?
When two different keys produce the same hash value, so they would be stored in the same position.
How does rehashing deal with a collision?
It uses a rule, such as moving to the next free slot, to find another position for the item.
With table size 10 and key % 10, which slot do 36 and 86 go to first?
Both go to slot 6, which is a collision. Whichever is inserted second must be rehashed to another slot, such as slot 7.