I think most of the existing hash functions were developed many years ago, by very smart people. It is probably not possible to improve hashing algorithms any longer, still, I had to try this idea below.
But first, as you know, a hash function is a function that converts any data of any size to a fixed sized number. It is widely used in, e.g., lookup tables.
In a previous article of mine, on chess engine programming1, I learned a very cool hashing technique for chess board states. Zobrist hashing5 proved to be able to give an almost unique hash code for every chess position in a game, even though there are almost infinitive states a chess game can be in. I think that we can use Zobrist algorithm to hash for example something more common like words. Results revealed at the end. (It worked!)
As you probably know, .NET already contains a hash function for strings. The method is called GetHashCode and it converts any object to an integer.
string
GetHashCode
I want to know if we can create a better function using Zobrist ideas and with less collisions.
A hash collision occurs when two different objects, in this case strings, result in the same hash code.
I'm using a list of 466,545 English words3. The function GetHashCode in .NET produces only 48 collisions on this data set, so it is probably good enough in most scenarios. If you are fine with 48 collisions, you don’t have to read on. Here are few examples of words with same .NET hash code.
You might argue that since the dataset is known, it would be very easy to create a perfect hash algorithm without collisions. We could just give each word its unique number.
However, that is not the purpose of this investigation. We want the hash function to work for any word in any language and with a very low probability of collisions.
Zobrist hashing works by bit-wise XOR-ing random numbers. In a chess engine, random numbers are generated for moves, and a move is defined by removing a piece from a square and then putting it on another.
There are two player colors, six piece types and 64 squares. 2 * 6 * 64 = 768 random numbers.
(Not taking into account castling and enpassant, but you get the point.)
When hashing words, I thought that we can generate random numbers for each character and then do the same thing as in Zobrist hashing. One bit-wise XOR operation for every character in a word.
public static int ZobHash(this string text)
{
var hash = ZobRandom.Start;
foreach (var c in text)
hash ^= ZobRandom.Numbers[c];
return hash;
}
But when doing this, I get 218,163 collisions. Not very encouraging, but instead of giving up, let’s look at why there are so many collisions.
Here are a few examples:
As you can see, Zobrist hash gives the same hash code for words independent of the order of the letters, i.e., the order of the XOR operations.
That is how XOR works and it's perfect for storing chess game states, because the order of moves that led to a board position doesn’t matter, it is still the same game state. But for hashing of words, it does not work.
Let’s try to extend Zobrist hash so the order of the letters also affects the resulting hash code.
What if we try to do a cyclic bit-shift (very fast operations) for each character random number?
A cyclic bit shift pushes the bits in a number to the left (or to the right if you want) like this:
This is the function for cyclic left bit shift.
private static uint RotateLeft(uint value, int count)
{
var r = count % 32;
return (value << r) | (value >> (32 - r));
}
And the final hash function looks like:
public static uint ZobHash(this string text)
{
var hash = ZobRandom.Start;
var i = 1;
foreach (var c in text)
hash ^= RotateLeft(ZobRandom.Numbers[c], i++);
return hash;
}
As I mentioned above, Zobrist hash uses a list of random numbers. For some reason, the function works better with some random numbers.
You might be unlucky to select a bad random seed that in the end generates more hash collisions.
So, implementing a Zobrist hashing function is also about selecting a good random seed.
Below is the result of counting performance and number of collisions for the list of 466,545 unique English words.
Also comparing with a few other hash functions from Brandon Dahler nuget System.Data.HashFunction4.
If you have a suggestion on more hash functions we should compare with, please comment below.
These are the results from hashing byte-arrays.
Functions with 32-bit hash codes:
Functions with 64-bit hash codes:
These are the results from hashing strings (a lot faster without conversion to byte-array):
ZobHash performs well compared to all tested functions. For functions that generate 32-bit hash codes, it has the lowest rate of collisions. There are functions that are faster, but they produce more collisions.
Interestingly .NET GetHashCode gives different number of collisions depending on if you hash a byte array or a string.
For 64-bit hash code functions, none of the tested functions generates collisions but ZobHash is the fastest.
That was all for now and don't forget to vote!
Thanks!
int
uint
This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)
As you probably know, .NET already contains a hash function for strings.
The method is called GetHashCode and it converts any object to an integer.
hash = crc32Tab[(uint8_t)hash ^ *data ] ^ (hash>>8);
return (value << count) | (value >> (32 - r))
return (int)(((uint)value << r) | (((uint)value) >> (32 - r)))
General News Suggestion Question Bug Answer Joke Praise Rant Admin
Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.