In computer programming, primary clustering is a phenomenon that causes performance degradation in linear-probing hash tables. The phenomenon states that, as elements are added to a linear probing hash table, they have a tendency to cluster together into long runs (i.e., long contiguous regions of the hash table that contain no free slots). If the hash table is at a load factor of 1 − 1 / x {\displaystyle 1-1/x} for some parameter x ≥ 2 {\displaystyle x\geq 2} , then the expected length of the run containing a given element u {\displaystyle u} is Θ ( x 2 ) {\displaystyle \Theta (x^{2})} . This causes insertions and negative queries to take expected time Θ ( x 2 ) {\displaystyle \Theta (x^{2})} in a linear-probing hash table.
Causes of primary clustering Primary clustering has two causes:
Winner keeps winning: The longer that a run becomes, the more likely it is to accrue additional elements. This causes a positive feedback loop that contributes to the clustering effect. However, this alone would not cause the quadratic blowup. Joining of runs: A single insertion may not only increase the length of the run that it is in by one, but may instead connect together two runs that were already relatively long. This is what causes the quadratic blowup in expected run length. Another way to understand primary clustering is by examining the standard deviation on the number of items that hash to a given region within the hash table. Consider a sub-region of the hash table of size x 2 {\displaystyle x^{2}} . The expected number of items that hash into the region is ( 1 − 1 / x ) x 2 = x 2 − x {\displaystyle (1-1/x)x^{2}=x^{2}-x} . On the other hand, the standard deviation on the number of such items is Θ ( x ) {\displaystyle \Theta (x)} . It follows that, with probability Ω ( 1 ) {\displaystyle \Omega (1)} , the number of items that hash into the region will exceed the size x 2 {\displaystyle x^{2}} of the region. Intuitively, this means that regions of size Θ ( x 2 ) {\displaystyle \Theta (x^{2})} will often overflow, while larger regions typically will not. This intuition is often used as the starting point for formal analyses of primary clustering.
Effect on performance Primary clustering causes performance degradation for both insertions and queries in a linear probing hash table. Insertions must travel to the end of a run, and therefore take expected time Θ ( x 2 ) {\displaystyle \Theta (x^{2})} . Negative queries (i.e., queries that are searching for an element that turns out not to be present) must also travel to the end of a run, and thus also take expected time Θ ( x 2 ) {\displaystyle \Theta (x^{2})} . Positive queries can terminate as soon as they find the element that they are searching for. As a result, the expected time to query a random element in the hash table is Θ ( x ) {\displaystyle \Theta (x)} . However, positive queries to recently-inserted elements (e.g., an element that was just inserted) take expected time Θ ( x 2 ) {\displaystyle \Theta (x^{2})} . These bounds also hold for linear probing with lazy deletions (i.e., using tombstones for deletions), as long as the hash table is rebuilt (and the tombstones are cleared out) semi-frequently. It suffices to perform such a rebuild at least once every n / ( 2 x ) {\displaystyle n/(2x)} insertions.
Common misconceptions Many textbooks describe the winner-keeps-winning effect (in which the longer a run becomes, the more likely it is to accrue additional elements) as the sole cause of primary clustering. However, as noted by Knuth, this is not the main cause of primary clustering. Some textbooks state that the expected time for a positive query is Θ ( x ) {\displaystyle \Theta (x)} , typically citing Knuth. This is true for a query to a random element. Some positive queries may have much larger expected running times, however. For example, if one inserts an element and then immediately queries that element, the query will take the same amount of time as did the insertion, which is Θ ( x 2 ) {\displaystyle \Theta (x^{2})} in expectation.
… excerpt ends here. Continue reading the full article.
