Computer Algorithms II Lecture Notes

23 September 2008 • Implementing Hash Tables


For example, the expression

  (hash(v, n) << 24) 
+ (hash(v, n) << 16) 
+ (hash(v, n) <<  8)
+ hash(v, n)

produces a four-byte hash value. This particular expression is bad because each call to hash() returns the same value, greatly degrading the randomness of the resulting multi-byte hash value.

There are a couple of ways to fix this problem. One is to have four hash functions, each with a different random permutation.

  (hash0(v, n) << 24) 
+ (hash1(v, n) << 16) 
+ (hash2(v, n) <<  8)
+ hash3(v, n)

Another is to change hash() to accept a seed used to initialize the hash value.

byte hash(byte v[], unsigned n, unsigned seed)
  byte h = rp[seed % 256]
  for i = 0 to n - 1
    h = h xor rp[v[i]]
  return h

and create multi-byte has values with

  (hash(v, n, 0) << 24) 
+ (hash(v, n, 1) << 16) 
+ (hash(v, n, 2) <<  8)
+ hash(v, n, 3)


This page last modified on 24 January 2006.