[英]Hashtable Implementation
I was recently asked 'how would you implement a hastable'. 我最近被问到'你将如何实施一个可憎的'。 I know the hashing algorithm is critical as the less collisions the better WRT performance, but what algorithm/data structure should be employed to deliver amortized constant time {O(1)} for insert/delete/lookups? 我知道哈希算法是关键的,因为较少的冲突会带来更好的WRT性能,但是应该使用什么算法/数据结构来为插入/删除/查找提供分摊的常量时间{O(1)}?
Hash tables have two main possibilities: 哈希表有两种主要可能性:
The important part about hash tables [in both solutions] in order to allow armotorized O(1) insert/del/look up - is allocating a bigger table and rehashing once a pre defined load factor was reached. 关于哈希表[在两个解决方案中]的重要部分,为了允许机动化的O(1)插入/ del /查找 - 分配更大的表并在达到预定义的加载因子后重新散列。
EDIT: complexity analsis: 编辑:复杂性analsis:
Assume a load factor of p
for some p < 1
. 对于某些p < 1
假设负载因子为p
。
p
Thus the mean of array accesses is: Sigma(i * p^(i-1) * (1-p)) for each i in [1,n]
This gives you: Sigma(i * p^(i-1) * (1-p)) = (1-p) * Sigma(i * p^(i-1)) <= (1-p) * 1/(p-1)^2 = 1-p = CONST
. 每次访问中“碰撞”的概率是p
因此,数组访问的平均值是: Sigma(i * p^(i-1) * (1-p)) for each i in [1,n]
这给出了: Sigma(i * p^(i-1) * (1-p)) = (1-p) * Sigma(i * p^(i-1)) <= (1-p) * 1/(p-1)^2 = 1-p = CONST
。 [have a look at the correctness of Sigma(i * p^(i-1)) < 1/(p-1)^2 in wolfram alpha ]. [看看Sigma(i * p ^(i-1))<1 /(p-1)^ 2在wolfram alpha中的正确性。 Thus resulting on average a constant number of accesses to the array. 因此平均导致对阵列的恒定访问次数。 Also: You might need to rehash all elements after the load factor was reached, resulting in O(n)
accesses to the array. 另外:在达到加载因子后,您可能需要重新散列所有元素,从而导致对数组的O(n)
访问。 This results in n * O(1)
ops [adding n elements] and 1 * O(n)
op [rehashing], so you get: (n * O(1) + 1 * O(n)) / n = O(1)
armotorized time. 这导致n * O(1)
ops [添加n个元素]和1 * O(n)
op [rehashing],因此得到: (n * O(1) + 1 * O(n)) / n = O(1)
机动时间。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.