简体   繁体   English

哈希表预期插入

[英]Hash Table Expected Insertions

I'm a bit stumped by a conjecture proposed in an Algorithm Analysis class I'm taking. 我对我正在参加的算法分析课程中提出的猜想感到有些困惑。 Here is the question. 这是问题。


Assume that we have a hash table structured as a vector of lists, resolving collisions by sequential search of buckets. 假设我们有一个哈希表构造为列表向量,可以通过顺序搜索存储桶来解决冲突。 Without making any assumptions on the hash function used, show the following, where n is the number of items in the table and b is the number of buckets (or slots) in the table implementation. 在不对使用的哈希函数进行任何假设的情况下,显示以下内容,其中n是表中项目的数量,b是表实现中的存储桶(或插槽)的数量。 e(k) denotes the expected number of buckets of size k and p(k) denotes the probability a given bucket has exactly k items. e(k)表示大小为k的存储桶的预期数量,p(k)表示给定存储桶恰好具有k个项目的概率。


I interpret the above description as basically describing a situation where n items are inserted into any one of b buckets. 我将以上描述解释为基本上描述了n个项目插入b个存储桶中的任何一个的情况。 A bucket can contain between n and 0 items. 一个存储桶可以包含n到0个项目。 It can be empty or it can contain all of the n items. 它可以为空,也可以包含所有n个项目。 We can make no assumptions about the p(k) function that denotes the probability that any one item will be inserted into any of the buckets. 我们无法对p(k)函数做出任何假设,该函数表示将任何一项插入任何存储桶的概率。

The conjecture that I need to prove is..... 我需要证明的猜想是.....


Show that e(k) = bp(k) 证明e(k)= bp(k)


That is, show that the expected number of buckets of size k is equal to the probability that any given bucket has k items multiplied by b buckets. 也就是说,证明大小为k的存储桶的预期数量等于任何给定存储桶具有k个项目乘以b个存储桶的概率。

I'd like to start this problem by saying that the probability that any one bucket receives an item is simply p(1) = 1/b. 首先,我想说一个问题:任何一个桶收到一个项目的概率就是p(1)= 1 / b。 But this doesn't work since I'd have to assume that the p(k) function distributes items into the buckets evenly, which it may not necessarily do. 但这是行不通的,因为我不得不假设p(k)函数将项平均分配到存储桶中,而这不一定是必须的。

I'm not exactly sure how to set this problem up with so little information. 我不确定如何用很少的信息来解决这个问题。 Help would be much appreciated. 帮助将不胜感激。

Well... For me, you just have to say that the probability for one bucket to have k elements is independent of the probability that any other bucket has k elements. 好吧...对我来说,您只需要说一个存储桶包含k个元素的概率与其他存储桶具有k个元素的概率无关。 Therefore 因此

e(k) = probability of bucket 1 having k elements
     + probability of bucket 2 having k elements
     + ...
     + probability of bucket b having k elements
     = b*p(k)

I am not sure that is it what you are waiting for. 我不确定这就是您在等待什么。 The result seems pretty straightforward to me. 结果对我来说似乎很简单。 Sorry if it did not help. 抱歉,如果没有帮助。

I think this is a great spot to use linearity of expectation. 我认为这是使用期望线性的好地方。

Let's define some new random variables. 让我们定义一些新的随机变量。 For any bucket i and capacity k, let X i, k be 1 if bucket i has k elements in it and 0 otherwise. 对于任何存储桶i和容量k,如果存储桶i中有k个元素,则令X i,k为1,否则为0。 This means that the total number of buckets with capacity k is given by 这意味着容量为k的铲斗总数为

X 1, k + X 2, k + ... + X n, k X 1,k + X 2,k + ... + X n,k

Therefore, e(k) is given by 因此,e(k)由

e(k) = E[X 1, k + X 2, k + ... + X n, k ] e(k)= E [X 1,k + X 2,k + ... + X n,k ]

via linearity of expectation, we have 通过期望的线性,我们有

e(k) = E[X 1, k ] + E[X 2, k ] + ... + E[X n, k ] e(k)= E [X 1,k ] + E [X 2,k ] + ... + E [X n,k ]

Now, let's look at any one of the X i, k variables. 现在,让我们看一下X i,k变量中的任何一个。 Notice that X i, k is 1 if bucket i has exactly k elements and is 0 otherwise. 请注意,如果存储区i恰好具有k个元素,则X i,k为1,否则为0。

Therefore: 因此:

E[X i, k ] = 0 · Pr[bucket i doesn't have k elements] + 1 · Pr[bucket i does have k elements] E [X i,k ] = 0·Pr [存储桶i没有k元素] + 1·Pr [存储桶i没有k元素]

= Pr[bucket i does have k elements] = Pr [存储桶i确实有k个元素]

And hey! 嘿! We know what that probability is - it's p(k). 我们知道那个概率是多少-它是p(k)。 Therefore, E[X i, k ] = p(k), so we have that 因此,E [X i,k ] = p(k),所以我们有

e(k) = E[X 1, k ] + E[X 2, k ] + ... + E[X n, k ] e(k)= E [X 1,k ] + E [X 2,k ] + ... + E [X n,k ]

= p(k) + p(k) + ... + p(k) = p(k)+ p(k)+ ... + p(k)

= np(k) = np(k)

and we're done! 我们完成了!

Hope this helps! 希望这可以帮助!

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

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