|
| 1 | +# Hash function |
| 2 | + |
| 3 | +In this section, we'll write our hash function. |
| 4 | + |
| 5 | +The hash function we choose should: |
| 6 | + |
| 7 | +- Take a string as its input and return a number between `0` and `m`, our |
| 8 | + desired bucket array length. |
| 9 | +- Return an even distribution of bucket indexes for an average set of inputs. If |
| 10 | + our hash function is unevenly distributed, it will put more items in some |
| 11 | + buckets than others. This will lead to a higher rate of |
| 12 | + [collisions](#collisions). Collisions reduce the efficiency of our hash table. |
| 13 | + |
| 14 | +## Algorithm |
| 15 | + |
| 16 | +We'll make use of a generic string hashing function, expressed below in |
| 17 | +pseudocode. |
| 18 | + |
| 19 | +``` |
| 20 | +function hash(string, a, num_buckets): |
| 21 | + hash = 0 |
| 22 | + string_len = length(string) |
| 23 | + for i = 0, 1, ..., string_len: |
| 24 | + hash += (a ** string_len - (i+1)) * char_code(string[i]) |
| 25 | + hash = hash % num_buckets |
| 26 | + return hash |
| 27 | +``` |
| 28 | + |
| 29 | +This hash function has two steps: |
| 30 | + |
| 31 | +1. Convert the string to a large integer |
| 32 | +2. Reduce the size of the integer to a fixed range by taking its remainder `mod` |
| 33 | + `m` |
| 34 | + |
| 35 | +The variable `a` should be a prime number larger than the size of the alphabet. |
| 36 | +We're hashing ASCII strings, which has an alphabet size of 128, so we should |
| 37 | +choose a prime larger than that. |
| 38 | + |
| 39 | +`char_code` is a function which returns an integer which represents the |
| 40 | +character. We'll use ASCII character codes for this. |
| 41 | + |
| 42 | +Let's try the hash function out: |
| 43 | + |
| 44 | +``` |
| 45 | +hash("cat", 151, 53) |
| 46 | +
|
| 47 | +hash = 151**2 * 99 + 151**1 * 97 + 151**0 * 116 % 53 |
| 48 | +hash = 2257299 + 14647 + 116 % 53 |
| 49 | +hash = 2272062 % 53 |
| 50 | +hash = 5 |
| 51 | +``` |
| 52 | + |
| 53 | +Changing the value of `a` give us a different hash function. |
| 54 | + |
| 55 | +``` |
| 56 | +hash("cat", 163, 53) = 3 |
| 57 | +``` |
| 58 | + |
| 59 | +## Implementation |
| 60 | + |
| 61 | +```c |
| 62 | +// hash_table.c |
| 63 | +static int ht_hash(const char* s, const int a, const int m) { |
| 64 | + long hash = 0; |
| 65 | + const int len_s = strlen(s); |
| 66 | + for (int i = 0; i < len_s; i++) { |
| 67 | + hash += (long)pow(a, len_s - (i+1)) * s[i]; |
| 68 | + hash = hash % m; |
| 69 | + } |
| 70 | + return (int)hash; |
| 71 | +} |
| 72 | +``` |
| 73 | +
|
| 74 | +## Pathological data |
| 75 | +
|
| 76 | +An ideal hash function would always return an even distribution. However, for |
| 77 | +any hash function, there is a 'pathological' set of inputs, which all hash to |
| 78 | +the same value. To find this set of inputs, run a large set of inputs through |
| 79 | +the function. All inputs which hash to a particular bucket form a pathological |
| 80 | +set. |
| 81 | +
|
| 82 | +The existence of pathological input sets means there are no perfect hash |
| 83 | +functions for all inputs. The best we can do is to create a function which |
| 84 | +performs well for the expected data set. |
| 85 | +
|
| 86 | +Pathological inputs also poses a security issue. If a hash table is fed a set of |
| 87 | +colliding keys by some malicious user, then searches for those keys will take |
| 88 | +much longer (`O(n)`) than normal (`O(1)`). This can be used as a denial of |
| 89 | +service attack against systems which are underpinned by hash tables, such as DNS |
| 90 | +and certain web services. |
| 91 | +
|
| 92 | +Next section: [Handling collisions](/collisions) |
| 93 | +[Table of contents](https://github.com/jamesroutley/write-a-hash-table#contents) |
0 commit comments