SLIDE 8 Visually (pseudo-code)
2 3 4 Hashmap m; m.add(“dog”, 100); m.add(“cat”, 50); m.add(“lion”, 200); hashFunction(“dog”) => 2398 % 5 = 3; hashFunction(“cat”) => 1371 % 5 = 1; hashFunction(“lion”) => 199121 % 5 = 1; We can make this array size more reasonable by using the modulus operator! Collision @ 1
Rather than having a sparsely populated, ginormous array we can force all of our hashes to fit within the size of any array using modulus. The downside of this is now our keys no longer have unique indexes, as both "ca" and "lion" have an index of 1. When two keys have the same index this is called a collision. The smaller the size of the internal array and the fuller the array is increase the chances of a collision happening. At the extreme, an array with size 1 or a full array will always encounter a collision when adding another element. There are a couple common schemes for dealing with collisions. The first one is known as open
- addressing. In this scheme when a collision occurs the value is placed in the next available location.
The downside is you now have to hunt for the item when trying to retrieve it from the hash table. Clusters, or several keys that have the same index, can quickly decrease the efficiency of the hash
- table. We won't be implementing open addressing in this course.
8