d-ary heap

From testwiki
Jump to navigation Jump to search

The Template:Math-ary heap or Template:Math-heap is a priority queue data structure, a generalization of the binary heap in which the nodes have Template:Math children instead of 2.[1][2][3] Thus, a binary heap is a 2-heap, and a ternary heap is a 3-heap. According to Tarjan[2] and Jensen et al.,[4] Template:Math-ary heaps were invented by Donald B. Johnson in 1975.[1]

This data structure allows decrease priority operations to be performed more quickly than binary heaps, at the expense of slower delete minimum operations. This tradeoff leads to better running times for algorithms such as Dijkstra's algorithm in which decrease priority operations are more common than delete min operations.[1][5] Additionally, Template:Math-ary heaps have better memory cache behavior than binary heaps, allowing them to run more quickly in practice despite having a theoretically larger worst-case running time.[6] Like binary heaps, Template:Math-ary heaps are an in-place data structure that use no additional storage beyond that needed to store the array of items in the heap.[2][7]

Data structure

The Template:Math-ary heap consists of an array of Template:Math items, each of which has a priority associated with it. These items may be viewed as the nodes in a complete Template:Math-ary tree, listed in breadth first traversal order: the item at position 0 of the array (using zero-based numbering) forms the root of the tree, the items at positions 1 through Template:Math are its children, the next Template:Math items are its grandchildren, etc. Thus, the parent of the item at position Template:Math (for any Template:Math) is the item at position Template:Math and its children are the items at positions Template:Math through Template:Math. According to the heap property, in a min-heap, each item has a priority that is at least as large as its parent; in a max-heap, each item has a priority that is no larger than its parent.[2][3]

The minimum priority item in a min-heap (or the maximum priority item in a max-heap) may always be found at position 0 of the array. To remove this item from the priority queue, the last item x in the array is moved into its place, and the length of the array is decreased by one. Then, while item x and its children do not satisfy the heap property, item x is swapped with one of its children (the one with the smallest priority in a min-heap, or the one with the largest priority in a max-heap), moving it downward in the tree and later in the array, until eventually the heap property is satisfied. The same downward swapping procedure may be used to increase the priority of an item in a min-heap, or to decrease the priority of an item in a max-heap.[2][3]

To insert a new item into the heap, the item is appended to the end of the array, and then while the heap property is violated it is swapped with its parent, moving it upward in the tree and earlier in the array, until eventually the heap property is satisfied. The same upward-swapping procedure may be used to decrease the priority of an item in a min-heap, or to increase the priority of an item in a max-heap.[2][3]

To create a new heap from an array of Template:Math items, one may loop over the items in reverse order, starting from the item at position Template:Math and ending at the item at position 0, applying the downward-swapping procedure for each item.[2][3]

Analysis

In a Template:Math-ary heap containing Template:Math items, both the upward-swapping procedure and the downward-swapping procedure may perform as many as Template:Math swaps. In the upward-swapping procedure, each swap involves a single comparison of an item with its parent, and takes constant time. Therefore, the time to insert a new item into the heap, to decrease the priority of an item in a min-heap, or to increase the priority of an item in a max-heap, is Template:Math. In the downward-swapping procedure, each swap involves Template:Math comparisons and takes Template:Math time: it takes Template:Math comparisons to determine the minimum or maximum of the children and then one more comparison against the parent to determine whether a swap is needed. Therefore, the time to delete the root item, to increase the priority of an item in a min-heap, or to decrease the priority of an item in a max-heap, is Template:Math.[2][3]

When creating a Template:Math-ary heap from a set of n items, most of the items are in positions that will eventually hold leaves of the Template:Math-ary tree, and no downward swapping is performed for those items. At most Template:Math items are non-leaves, and may be swapped downwards at least once, at a cost of Template:Math time to find the child to swap them with. At most Template:Math nodes may be swapped downward two times, incurring an additional Template:Math cost for the second swap beyond the cost already counted in the first term, etc. Therefore, the total amount of time to create a heap in this way is

i=1logdn(ndi+1)O(d)=O(n).[2][3]

The exact value of the above (the worst-case number of comparisons during the construction of d-ary heap) is known to be equal to:

dd1(nsd(n))(d1(nmodd))(ed(nd)+1),[8]

where sd(n) is the sum of all digits of the standard base-d representation of n and ed(n) is the exponent of d in the factorization of n. This reduces to

2n2s2(n)e2(n),[8]

for d = 2, and to

32(ns3(n))2e3(n)e3(n1),[8]

for d = 3.

The space usage of the Template:Math heap, with insert and delete-min operations, is linear, as it uses no extra storage other than an array containing a list of the items in the heap.[2][7] If changes to the priorities of existing items need to be supported, then one must also maintain pointers from the items to their positions in the heap, which again uses only linear storage.[2]

Applications

When operating on a graph with Template:Mvar edges and Template:Mvar vertices, both Dijkstra's algorithm for shortest paths and Prim's algorithm for minimum spanning trees use a min-heap in which there are Template:Math delete-min operations and as many as Template:Mvar decrease-priority operations. By using a Template:Mvar-ary heap with Template:Math, the total times for these two types of operations may be balanced against each other, leading to a total time of Template:Math for the algorithm, an improvement over the Template:Math running time of binary heap versions of these algorithms whenever the number of edges is significantly larger than the number of vertices.[1][5] An alternative priority queue data structure, the Fibonacci heap, gives an even better theoretical running time of Template:Math, but in practice Template:Mvar-ary heaps are generally at least as fast, and often faster, than Fibonacci heaps for this application.[9]

4-heaps may perform better than binary heaps in practice, even for delete-min operations.[2][3] Additionally, a Template:Mvar-ary heap typically runs much faster than a binary heap for heap sizes that exceed the size of the computer's cache memory;[10] this may be due to the binary heap implicating more cache misses or virtual memory page faults, which can consume more processing time than the extra work of the few additional comparisons at each level entailed by a Template:Mvar-ary heap.[6][11]

References

Template:Reflist

  1. 1.0 1.1 1.2 1.3 Template:Citation.
  2. 2.00 2.01 2.02 2.03 2.04 2.05 2.06 2.07 2.08 2.09 2.10 2.11 Template:Citation. Note that Tarjan uses 1-based numbering, not 0-based numbering, so his formulas for the parent and children of a node need to be adjusted when 0-based numbering is used.
  3. 3.0 3.1 3.2 3.3 3.4 3.5 3.6 3.7 Template:Citation.
  4. Template:Citation.
  5. 5.0 5.1 Template:Harvtxt, pp. 77 and 91.
  6. 6.0 6.1 Template:Citation.
  7. 7.0 7.1 Template:Citation.
  8. 8.0 8.1 8.2 Template:Citation.
  9. Template:Citation
  10. Template:Cite journal
  11. Template:Citation.