Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Hash function for a string

We are currently dealing with hash function in my class. Our instructor asked us to a hash function on the internet to compare to the two we have used in our code.

The first one:

int HashTable::hash (string word)    // POST: the index of entry is returned {       int sum = 0;         for (int k = 0; k < word.length(); k++)             sum = sum + int(word[k]);         return  sum % SIZE;  } 

Second:

int HashTable::hash (string word) {    int seed = 131;     unsigned long hash = 0;    for(int i = 0; i < word.length(); i++)    {       hash = (hash * seed) + word[i];    }    return hash % SIZE; } 

Where SIZE is 501 (The size of the hash table) and the input is coming from a text file of 20,000+ words.

I saw this question with a few code examples but wasn't exactly sure what to be looking for in a hash function. If I understand correctly, in my case, a hash takes an input (string) and does a math calculation to assign the string a number and inserts it in a table. This process is done to increase the speed of searching the list?

If my logic is sound, does anyone have a good example or a resource showing a different hash function that involves a string? Or even the process of writing my own efficient hash function.

like image 568
Nick Avatar asked Nov 29 '11 20:11

Nick


People also ask

How do you hash a string?

The process of hashing in cryptography is to map any string of any given length, to a string with a fixed length. This smaller, fixed length string is known as a hash. To create a hash from a string, the string must be passed into a hash function.

What would the hash function return for the string?

A Hash function is a function that maps any kind of data of arbitrary size to fixed-size values. The values returned by the function are called Hash Values or digests.

What is a good hash function for strings C++?

If you just want to have a good hash function, and cannot wait, djb2 is one of the best string hash functions i know. it has excellent distribution and speed on many different sets of keys and table sizes. you are not likely to do better with one of the "well known" functions such as PJW, K&R[1], etc.

What does it mean to hash a string?

Hashing is the process of transforming any given key or a string of characters into another value. This is usually represented by a shorter, fixed-length value or key that represents and makes it easier to find or employ the original string. The most popular use for hashing is the implementation of hash tables.


1 Answers

First, it usually does not matter that much in practice. Most hash functions are "good enough".

But if you really care, you should know that it is a research subject by itself. There are thousand of papers about that. You can still get a PhD today by studying & designing hashing algorithms.

Your second hash function might be slightly better, because it probably should separate the string "ab" from the string "ba". On the other hand, it is probably less quick than the first hash function. It may, or may not, be relevant for your application.

I'll guess that hash functions used for genome strings are quite different than those used to hash family names in telephone databases. Perhaps even some string hash functions are better suited for German, than for English or French words.

Many software libraries give you good enough hash functions, e.g. Qt has qhash, and C++11 has std::hash in <functional>, Glib has several hash functions in C, and POCO has some hash function.

I quite often have hashing functions involving primes (see Bézout's identity) and xor, like e.g.

#define A 54059 /* a prime */ #define B 76963 /* another prime */ #define C 86969 /* yet another prime */ #define FIRSTH 37 /* also prime */ unsigned hash_str(const char* s) {    unsigned h = FIRSTH;    while (*s) {      h = (h * A) ^ (s[0] * B);      s++;    }    return h; // or return h % C; } 

But I don't claim to be an hash expert. Of course, the values of A, B, C, FIRSTH should preferably be primes, but you could have chosen other prime numbers.

Look at some MD5 implementation to get a feeling of what hash functions can be.

Most good books on algorithmics have at least a whole chapter dedicated to hashing. Start with wikipages on hash function & hash table.

like image 188
Basile Starynkevitch Avatar answered Oct 07 '22 10:10

Basile Starynkevitch