0

This code (extraced from an LZW compression program of unknown origin) finds an empty slot in a hash table of size 5021, indexed from 0 to 5020:

probe := <random 12-bit hash key>
// probe is initially 0 to 4095
repeat
  {
  if table[probe] is empty then return(probe);
  if probe == 0 then probe := -1 else dec(probe, 5021-probe);
  if probe < 0 then inc(probe, 5021);
  }

This isn't quite the typical linear or quadratic probing. Why probe like that? Is this a known probing algorithm, and where can I find out more about it?

1 Answers1

0

The algorithm for calculating the new probe is simple despite its ugly looks:

if probe == 0
    probe <= 5020
else
    probe <= (2*probe) % 5021

It is not quite clear why this function has been picked, but it really does go through all possible positions 1..5020 in a cyclic and seemingly random way (and 0 is sent back to the loop). (No, it doesnt, see the oops!) It should be noted that number 5021 is slightly magical in this context.

This algorithm is actually a linear congruential generator (LCG). See http://en.wikipedia.org/wiki/Linear_congruential_generator

OOPS: It is a LCG, but not one with the optimal period, because 2^1004 % 5021 == 1. There are five different cycles with the following members: (1, 2, 4, 8, ...), (3, 6, 12, 24, ...), (7, 14, 28, 56, ...), (9, 18, 36, 72, ...), and (11, 22, 44, 88, ...). Even more odd someone has chosen to use this algorithm. (Or then I analysed it wrong.)

DrV
  • 22,637
  • 7
  • 60
  • 72
  • What's magical about 5021 here? 5021 is prime, but 2 seems to have 1004 modulo 5021... – tmyklebu Jun 18 '14 at 22:06
  • 5021 is not magical in the LCG, as it's just the size of the hash table. The size of the hash table, on the other hand, might be magical. But, another thing: Why does it go through all possible positions? The parameters are a = 2, c = 0, m = 5021, which fails the criteria of the Hull-Dobell theorem (a-1 is not divisible by m, and c is not coprime to m). –  Jun 18 '14 at 22:40
  • @tmyklebu I thought 5021 was chosen so that 2^i % 5021 == 1 only if i is divisible by 5020. Choosing such numbers is possible but not trivial. However, it turned out that my assumption was wrong. There is no magic behind 5021. – DrV Jun 19 '14 at 07:08
  • @delnan With c=0 the question of course boils down to primitive roots. But as I noticed a bit late, 2 is not a primitive root of 5021. By saying "slightly magical", I assumed 5021 had been chosen so that 2 is a primitive root of it. a=2 in this case is most probably fixed due to arithmetic constraints (only addition and subtraction in use). – DrV Jun 19 '14 at 07:11