简体   繁体   English

为什么在最坏的情况下,unorded_set重新哈希复杂度可能为O(n ^ 2)?

[英]Why unorded_set rehash complexity could be O(n^2) in worst case?

I don't understand why it is not linear. 我不明白为什么它不是线性的。

There is a good answer for similar question about multiset: why hastable's rehash complexity may be quadratic in worst case 对于多集的类似问题,有一个很好的答案: 为什么在最坏的情况下hastable的重新哈希复杂度可能是平方的

But what about set? 但是集合呢? It could be only one element for each key. 每个键只能是一个元素。

Update: 更新:

Many keys in one bucket is also not a problem. 一个存储桶中的许多键也不是问题。 We can go through them in a linear time. 我们可以在线性时间内浏览它们。

I think the right answer as mentioned below is that O(n^2) rehash complexity included in the standard to allow open adressing(and may be some other) implementation. 我认为如下所述,正确的答案是标准中包含O(n ^ 2)重新哈希复杂度,以允许开放地址(可能是其他地址)实现。

Basically, it would be possible to build a hash set that has O(n) worst case rehash time. 基本上,有可能构建具有O(n)最坏情况重新哈希时间的哈希集。 It would even be possible to build a multiset with this property that still grants the same guarantee that elements with the same key are behind each other in a bucket, so the link you state is wrong . 甚至有可能使用此属性构建一个多集 ,该多集仍然可以确保具有相同键的元素在存储桶中彼此后面,因此您声明的链接是错误的 (Well, not fully wrong, it admits that there may be O(n) implementations) (嗯,不是完全错误,它承认可能有O(n)实现)

It works like this: 它是这样的:

for each bucket b of old table
    for each element e in bucket b
        b' = new bucket of e
        prepend e before the first entry in b' // <---- this is the key optimization

The algorithm works for sets and multisets and is extremely simple. 该算法适用于集和多集,并且非常简单。 Instead of appending elements to the new bucket (which would be O(number of elements in the bucket) ) we prepend to the bucket which is O(1) (simply change two pointers). 而不是将元素追加到新存储桶(该存储桶将为O(number of elements in the bucket) ),而是添加到存储桶即O(1) (只需更改两个指针)。

Of course, this will reverse the elements in the bucket. 当然,这将反转存储桶中的元素。 But this is okay since the key multimap assumption that equal elements are behind each other still holds. 但这是可以的,因为关键的多图假设仍然存在相等的元素。

But beware of open addressing 但要注意开放式寻址

My solution only works for hashing with chaining. 我的解决方案仅适用于使用链式哈希。 It does not work for open addressing. 它不适用于开放式寻址。 Thus, since the spec surely wants to allow both implementation methods, it must state that O(n²) might be the worst case, even if there are implementations that have better asymptotic runtimes. 因此,由于规范肯定要允许两种实现方法,因此即使某些实现具有更好的渐近运行时,它也必须声明O(n²)可能是最坏的情况。

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

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