简体   繁体   English

调整时间复杂度 - 使用动态数组进行链式哈希

[英]resizing time complexity - Chain Hashing with Dynamic Array

we built a hash table that is a dynamic array and each element in it is a linked list(chain hashing).我们构建了一个 hash 表,它是一个动态数组,其中的每个元素都是一个链表(链散列)。

if each time we resize the hash table to be twice its original size, we obviously need to move all existing nodes in the chains, to the newly sized hash table, my question is: if we go through each linked list(the chains) -even the empty ones- does this change the complexity of amortized O(1)?如果每次我们将 hash 表的大小调整为原始大小的两倍,我们显然需要将链中的所有现有节点移动到新大小的 hash 表中,我的问题是:如果我们 Z34D1F91FB2E514B8572B2E514B8572B 到每个链表即使是空的——这会改变摊销 O(1) 的复杂性吗? if yes is going through the none empty linked lists a good solution(amortized time complexity of O(1))?如果是,则通过非空链接列表是一个很好的解决方案(O(1) 的摊销时间复杂度)?

Assuming you maintain a load factor of x < 1 (A good default is keeping 0.25 < x < 0.5 , where x is the load factor), and you calculate it based on the number of elements in the hash table, you are fine.假设您保持x < 1负载因子(一个好的默认值是保持0.25 < x < 0.5 ,其中x是负载因子),并且您根据 hash 表中的元素数计算它,您很好。

You are going to iterate the table, which will take linear time (<4*n iterations), and this will take place after at least 1/4*n operations - which means your amortized time is at worst ~4*n / (1/4*n) = 16 , which is still constant (This accounts for iterating over empty cells as well).您将迭代该表,这将花费线性时间(<4*n 次迭代),这将在至少1/4*n次操作后发生 - 这意味着您的摊销时间最差~4*n / (1/4*n) = 16 ,它仍然是常数(这也说明了对空单元格的迭代)。

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

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