简体   繁体   English

高效的HEAPIFY方法减少比较次数

[英]Efficient HEAPIFY method to reduce number of comparisons

Consider a binary max-heap with n elements.考虑一个有n元素的二元最大堆。 It will have a height of O(log n) .它的高度为O(log n) When new elements are inserted into the heap, they will be propagated in the heap so that max-heap property is satisfied always.当新元素插入堆中时,它们将在堆中传播,以便始终满足 max-heap 属性。

The new element will be added as the child on the last level.新元素将作为子元素添加到最后一层。 But post insertion, there can be violation of max-heap property.但是插入后,可能会违反最大堆属性。 Hence, heapify method will be used.因此,将使用 heapify 方法。 This will have a time complexity of O(log n) ie height of the heap.这将具有O(log n)的时间复杂度,即堆的高度。

But can we make it even more efficient?但是我们可以让它更有效率吗?
When multiple insert and delete are performed, this procedure makes things slow.当执行多个插入和删除时,此过程会使事情变慢。 Also, it is a strict requirement that the heap should be a max-heap post every insertion.此外,严格要求堆应该是每次插入后的最大堆。

The objective is to reduce the time complexity of heapify method.目的是降低heapify方法的时间复杂度。 This is possible only when the number of comparisons are reduced.这只有在减少比较次数时才有可能。

The objective is to reduce the time complexity of the heapify method.目的是降低heapify方法的时间复杂度。

That is a pity, because that is impossible, in contrast to很可惜,因为那是不可能的,相比之下

Reduce the time complexity of multiple inserts and deletes :降低多次插入和删除的时间复杂度

Imagine not inserting into the n item heap immediately,想象一下不立即插入到第n 个项目堆中,
building an auxiliary one (or even a list).建立一个辅助的(甚至是一个列表)。
On delete (extract?), place one item from the auxiliary (now at size k ) "in the spot emptied" and do a sift-down or up as required if k << n .在删除(提取?)时,将辅助项(现在大小为k )中的一项放在“清空的地方”,如果 k << n 则根据需要进行向下或向上筛选。
If the auxiliary data structure is not significantly smaller than the main one, merge them.如果辅助数据结构没有明显小于主数据结构,则合并它们。

Such ponderings lead to advanced heaps like Fibonacci, pairing , Brodal…这样的思考导致高级堆,如斐波那契、配对、布罗达尔……

The time complexity of the insert operation in a heap is dependent on the number of comparisons that are made.堆中插入操作的时间复杂度取决于进行比较的次数。 One can imagine to use some overhead to implement a smart binary search along the leaf-to-root path.可以想象使用一些开销来实现沿叶到根路径的智能二进制搜索。

However, the time complexity is not only determined by the number of comparisons.但是,时间复杂度不仅仅由比较的次数决定。 Time complexity is determined by any work that must be performed, and in this case the number of writes is also O(log) and that number of writes cannot be reduced.时间复杂度由必须执行的任何工作决定,在这种情况下,写入次数也是 O(log),并且无法减少写入次数。

The number of nodes whose value need to change by the insert operation is O(log).插入操作需要更改其值的节点数为 O(log)。 A reduction of the number of comparisons is not enough to reduce the complexity.减少比较次数不足以降低复杂性。

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

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