Jump to content

Shadow heap

From Wikipedia, the free encyclopedia

This is an old revision of this page, as edited by BG19bot (talk | contribs) at 06:58, 10 April 2014 (WP:CHECKWIKI error fix for #61. Punctuation goes before References. Do general fixes if a problem exists. - using AWB (10072)). The present address (URL) is a permanent link to this revision, which may differ significantly from the current revision.

In computer science, a shadow heap is a mergeable heap data structure which supports efficient heap merging in the amortized sense. More specifically, shadow heaps make use of the shadow merge algorithm to achieve insertion in O(f(n)) amortized time and deletion in O((log n log log n)/f(n)) amortized time, for any choice of 1 ≤ f(n) ≤ log log n.[1]

Throughout this article, it is assumed that A and B are binary heaps with |A| ≤ |B|.

Shadow merge

Shadow merge is an algorithm for merging two binary heaps efficiently if these heaps are implemented as arrays. Specifically, the running time of shadow merge on two heaps and is .

Algorithm

We wish to merge the two binary min-heaps and . The algorithm is as follows:

  1. Concatenate the array at the end of the array to obtain an array .
  2. Identify the shadow of in ; that is, the ancestors of the last nodes in which destroy the heap property.
  3. Identify the following two parts of the shadow from :
    • The path : the set of nodes in the shadow for which there are at most 2 at any depth of ;
    • The subtree : the remainder of the shadow.
  4. Extract and sort the smallest nodes from the shadow into an array .
  5. Transform as follows:
    • If , then starting from the smallest element in the sorted array, sequentially insert each element of into , replacing them with 's smallest elements.
    • If , then extract and sort the smallest elements from , and merge this sorted list with .
  6. Replace the elements of into their original positions in .
  7. Make a heap out of .

Running time

Again, let denote the path, and denote the subtree of the concatenated heap . The number of nodes in is at most twice the depth of , which is . Moreover, the number of nodes in at depth is at most 3/4 the number of nodes at depth , so the subtree has size . Since there are at most 2 nodes at each level on , then reading the smallest elements of the shadow into the sorted array takes time.

If , then combining and as in step 5 above takes time . Otherwise, the time taken in this step is . Finally, making a heap of the subtree takes time. This amounts to a total running time for shadow merging of .

Structure

A shadow heap consists of threshold function , and an array for which the usual array-implemented binary heap property is upheld in its first entries, and for which the heap property is not necessarily upheld in the other entries. Thus, the shadow heap is essentially a binary heap adjacent to an array . To add an element to the shadow heap, place it in the array . If the array becomes too large according to the specified threshold, we first build a heap out of using Floyd's algorithm for heap construction,[2] and then merge this heap with using shadow merge. Finally, the merging of shadow heaps is simply done through sequential insertion of one heap into the other using the above insertion procedure.

Analysis

We are given a shadow heap , with threshold function as above. Suppose that the threshold function is such that any change in induces no larger a change than in . We derive the desired running time bounds for the mergeable heap operations using the potential method for amortized analysis. The potential of the heap is chosen to be:

Using this potential, we can obtain the desired amortized running times:

create(H): initializes a new empty shadow heap

Here, the potential is unchanged, so the amortized cost of creation is , the actual cost.

insert(x, H): inserts into the shadow heap

There are two cases:
  • If the merge is employed, then the drop in the potential function is exactly the actual cost of merging and , so the amortized cost is .
  • If the merge is not done, then the amortized cost is
By choice of the threshold function, we thus obtain that the amortized cost of insertion is:

delete_min(H): deletes the minimum priority element from

Finding and deleting the minimum takes actual time . Moreover, the potential function can only increase after this deletion if the value of decreases. By choice of , we have that the amortized cost of this operation is the same as the actual cost.

A naive binary heap merging algorithm will merge the two heaps and in in time by simply concatenating both heaps and making a heap out of the resulting array using Floyd's algorithm for heap construction. Alternatively, the heaps can simply be merged by sequentially inserting each element of into , taking time .

Sack and Strothotte proposed an algorithm for merging the binary heaps in time.[3] Their algorithm is known to be more efficient than the second naive solution described above roughly when . Shadow merge performs asymptotically better than their algorithm, even in the worst case.

There are several other heaps which support faster merge times. For instance, Fibonacci heaps can be merged in time. Since binary heaps require time to merge,[4] shadow merge remains efficient.

References

  1. ^ Kuszmaul, Christopher L. (2000). Efficient Merge and Insert Operations for Binary Heaps and Trees (PDF) (Technical report). NASA Advanced Supercomputing Division. 00-003.
  2. ^ Suchenek, Marek A. (2012), "Elementary Yet Precise Worst-Case Analysis of Floyd's Heap-Construction Program", Fundamenta Informaticae, 120 (1), IOS Press: 75–92, doi:10.3233/FI-2012-751
  3. ^ Sack, Jörg-R.; Strothotte, Thomas (1985), "An Algorithm for Merging Heaps", Acta Informatica, 22 (2), Springer-Verlag: 171–186, doi:10.1007/BF00264229.
  4. ^ Cormen, Thomas H.; Leiserson, Charles E.; Rivest, Ronald L. (1990). Introduction to Algorithms (1st ed.). MIT Press and McGraw-Hill. ISBN 0-262-03141-8.