views:

355

answers:

5

I've done a little research on hash tables, and I keep running across the rule of thumb that when there are a certain number of entries (either max or via a load factor like 75%) the hash table should be expanded.

Almost always, the recommendation is to double (or double plus 1, i.e., 2n+1) the size of the hash table. However, I haven't been able to find a good reason for this.

Why double the size, rather than, say, increasing it 25%, or increasing it to the size of the next prime number, or next k prime numbers (e.g., three)?

I already know that it's often a good idea to choose an initial hash table size which is a prime number, at least if your hash function uses modulus such as universal hashing. And I know that's why it's usually recommended to do 2n+1 instead of 2n (e.g., http://www.concentric.net/~Ttwang/tech/hashsize.htm)

However as I said, I haven't seen any real explanation for why doubling or doubling-plus-one is actually a good choice rather than some other method of choosing a size for the new hash table.

(And yes I've read the Wikipedia article on hash tables :) http://en.wikipedia.org/wiki/Hash_table

+2  A: 

Doubling the memory when expanding any type of collection is an oftenly used strategy to prevent memory fragmentation and not having to reallocate too often. As you point out there might be reasons to have a prime number of elements. When knowing your application and your data, you might also be able to predict the growth of the number of elements and thus choose another (larger or smaller) growth factor than doubling.

The general implementations found in libraries are exactly that: General implementations. They have to focus on being a reasonable choice in a variety of different situations. When knowing the context, it is almost always possible to write a more specialized and more efficient implementation.

Anders Abel
+12  A: 

Hash-tables could not claim "amortized constant time insertion" if, for instance, the resizing was by a constant increment. In that case the cost of resizing (which grows with the size of the hash-table) would make the cost of one insertion linear in the total number of elements to insert. Because resizing becomes more and more expensive with the size of the table, it has to happen "less and less often" to keep the amortized cost of insertion constant.

Most implementations allow the average bucket occupation to grow to until a bound fixed in advance before resizing (anywhere between 0.5 and 3, which are all acceptable values). With this convention, just after resizing the average bucket occupation becomes half that bound. Resizing by doubling keeps the average bucket occupation in a band of width *2.

Sub-note: because of statistical clustering, you have to take an average bucket occupation as low as 0.5 if you want many buckets to have at most one elements (maximum speed for finding ignoring the complex effects of cache size), or as high as 3 if you want a minimum number of empty buckets (that correspond to wasted space).

Pascal Cuoq
@andras Right. 1.5 and 3 and just examples of reasonable values between which the doubling strategy makes the load factor vary. I took the values I knew to be used in my favorite language, but there's nothing special about them.
Pascal Cuoq
@andras I changed the formulation. If you looked it up, out of curiosity, what are the values for .Net and Java?
Pascal Cuoq
@andras because then, the amortized insertion time will in practice be dominated by the cost of resizing (you have to compute again the hash of all stored values when you resize). Sure, you could use 1.5 or 3, a factor of 2 just happens to balance the various costs in an acceptable manner. This is purely heuristic, any factor >1 gives the same asymptotic complexity. And even if you know what kind of memory/speed trade-off you are aiming for, there is no optimal value because everything depends on the costs of the provided hash and equality functions anyway.
Pascal Cuoq
@andras Well, if you get to the bottom of it, yes, it's the time taken by the numerous resize operations on the one hand, and the space wasted just after a resize on the other hand. Why did you delete the comment with the values in .Net and Java?
Pascal Cuoq
@andras It's really too bad the new version is inferior, because I edited it (repeatedly) on your suggestion. Have you considered the idea of editing your **own** answer to give us your views on hash tables? PS: feel free to vote this answer as you see fit.
Pascal Cuoq
@Pascal: I suggested you remove the parts about the load factors. For one, values outside 0.5 and 3 can just as well be acceptable depending on your needs. For two: a load factor of 3 is clearly not possible with an open addressing hash table that are just as common as chaining ones. For three: no, you do not need to take an average bucket occupation as low as 0.5. In fact, you may be happy with a minimum load factor of 1 with your chaining hash table and a value of 0.25 for your open addressing one. Open addressing ones usually operate with a load factor below or at 3/4.
andras
@Pascal: I did not suggest that doubling will not keep the load factor between bounds. I suggested that any number would keep it between bounds. Certainly, a growth factor of 1.1 will keep it between even tighter bounds. So what you state is clearly not the reason for choosing 2.
andras
+1  A: 

If you don't know how many objects you will end up using (lets say N),
by doubling the space you'll do log2N reallocations at most.

I assume that if you choose a proper initial "n", you increase the odds
that 2*n + 1 will produce prime numbers in subsequent reallocations.

Nick D
+2  A: 

The same reasoning applies for doubling the size as for vector/ArrayList implementations, see this answer.

Pete Kirkham
+1. Exactly so.
andras
A: 

I had read a very interesting discussion on growth strategy on this very site... just cannot find it again.

While 2 is commonly used, it's been demonstrated that it was not the best value. One often cited problem is that it does not cope well with allocators schemes (which often allocate power of twos blocks) since it would always require a reallocation while a smaller number might in fact be reallocated in the same block (simulating in-place growth) and thus being faster.

Thus, for example, the C++ Standard Library uses a growth factor of 1.5 (ideally should be the golden number) after an extensive discussion on the mailing list.

Of course, it should be tailored to the memory allocation strategy.

Matthieu M.