简体   繁体   English

散列数据结构

[英]Hashing in data structures

Consider a hash table with n buckets, where external (overflow) chaining is used to resolve collisions. 考虑具有n个存储桶的哈希表,其中使用外部(溢出)链接来解决冲突。 The hash function is such that the probability that a key value is hashed to a particular bucket is 1/n. 哈希函数使得键值被哈希到特定存储桶的概率为1 / n。 The hash table is initially empty and K distinct values are inserted in the table. 哈希表最初是空的,并且在表中插入了K个不同的值。

  1. What is the probability that bucket number 1 is empty after the Kth insertion? 第K个插入后,编号1的存储桶为空的概率是多少?
  2. What is the probability that no collision has occurred in any of the K insertions? 在K个插入中的任何一个中没有发生碰撞的概率是多少?
  3. What is the probability that the first collision occurs at the Kth insertion? 在第K次插入时发生第一次碰撞的概率是多少?
  1. The probability that bucket 1 is empty after ONE insertion is (n−1)/n . 一次插入后,存储桶1为空的概率为(n−1)/n That's the probability that the first item didn't hash to bucket 1. The event that it's empty after TWO insertions is defined by "first item missed bucket 1" AND "2nd item missed bucket one" which is (n - 1) * (n - 1) / n * n . 这就是第一项没有散列到存储桶1的可能性。两次插入后它为空的事件由“第一项丢失存储桶1”和“第二项丢失存储桶一个”定义为(n - 1) * (n - 1) / n * n With this, I hope you can compute the probability that the bucket's empty after K insertions. 借此,我希望您可以计算出K插入之后存储桶为空的概率。

  2. For K = 1 , it's 1 . 对于K = 1 ,它是1 For K = 2 , the second item must miss the bucket of the first item. 对于K = 2 ,第二个项目必须错过第一个项目的存储桶。 So it has n − 1 places it can safely go. 因此它可以安全地进入n − 1地方。 The probability of success is therefore (n − 1) / n . 因此,成功的概率为(n − 1) / n What about the third item? 那第三项呢? It has only n−2 places it can go. 它只能有n−2位置。 So the probability for K = 3 is (n − 1) * (n - 2) / n * n . 因此, K = 3的概率为(n − 1) * (n - 2) / n * n You can generalize. 您可以概括。 Be careful of the case K > n . 注意K > n的情况。

  3. Once you work out the details of the first two parts, you can probably make progress on this one as well. 一旦确定了前两部分的细节,您就可以在这部分上取得进展。

    Hint: the first collision occurs on the k th insertion if (i) the first k−1 insertions didn't collide (see 2) and (ii) the k th insertion DOES cause a collision (see the complement of 2). 提示:第一碰撞发生在k个插入如果(i)所述第一k−1插入没有碰撞(见图2)和(ii)所述k个插入DOES引起冲突(参见2的补)。

Let me know if you can figure out all the three answers. 让我知道您是否能找出所有三个答案。 Otherwise, I will put more details. 否则,我将详细介绍。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM