Hash tables are everywhere â behind every dictionary, cache, and database index you have ever used. The idea is deceptively simple: run a key through a hash function, land on a slot, and the lookup is done in constant time. Most of the time, at least.
The trouble is collisions. Two keys can hash to the same slot, or a whole cluster of keys can pile up in adjacent slots. One key might reach its home slot in a single step while another must probe ten slots before it finds a free one. That imbalance slows lookups for the unlucky keys and, at high load factors, makes the table uncomfortably slow.
Robin Hood hashing, introduced by Pedro Celis in his 1986 doctoral thesis, fixes this with a single elegant rule added to ordinary linear probing: during insertion, if the incoming key has traveled farther from its home than the key currently sitting in the candidate slot, swap them. The newcomer â the "poor" key â takes the slot; the displaced key â the "rich" one â continues probing.
The name is deliberate. The algorithm steals slots from keys that got lucky (short probe distances) and gives them to keys that got unlucky (long probe distances). The result is a dramatically flatter distribution of probe lengths, with a provably small maximum probe length. Unlike the average case (which ordinary probing already keeps reasonable), Robin Hood cares about the worst individual key â and it wins there too.
Comments
Loading comments...