Jump to content

d-ary heap

From Wikipedia, the free encyclopedia

Thed-ary heapord-heapis apriority queuedata structure,a generalization of thebinary heapin which the nodes havedchildren instead of 2.[1][2][3]Thus, a binary heap is a 2-heap, and aternary heapis a 3-heap. According to Tarjan[2]and Jensen et al.,[4]d-ary heaps were invented byDonald B. Johnsonin 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 asDijkstra's algorithmin which decrease priority operations are more common than delete min operations.[1][5]Additionally,d-ary heaps have bettermemory cachebehavior than binary heaps, allowing them to run more quickly in practice despite having a theoretically larger worst-case running time.[6]Like binary heaps,d-ary heaps are anin-place data structurethat uses no additional storage beyond that needed to store the array of items in the heap.[2][7]

Data structure[edit]

Thed-ary heap consists of anarrayofnitems, each of which has a priority associated with it. These items may be viewed as the nodes in a completed-ary tree, listed inbreadth first traversal order:the item at position 0 of the array (usingzero-based numbering) forms the root of the tree, the items at positions 1 throughdare its children, the nextd2items are its grandchildren, etc. Thus, the parent of the item at positioni(for anyi> 0) is the item at position(i− 1)/dand its children are the items at positionsdi+ 1throughdi+d.According to theheap 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 itemxin the array is moved into its place, and the length of the array is decreased by one. Then, while itemxand its children do not satisfy the heap property, itemxis 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 ofnitems, one may loop over the items in reverse order, starting from the item at positionn− 1and ending at the item at position 0, applying the downward-swapping procedure for each item.[2][3]

Analysis[edit]

In ad-ary heap withnitems in it, both the upward-swapping procedure and the downward-swapping procedure may perform as many aslogdn= logn/ logdswaps. 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, isO(logn/ logd).In the downward-swapping procedure, each swap involvesdcomparisons and takesO(d)time: it takesd− 1comparisons 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, isO(dlogn/ logd).[2][3]

When creating ad-ary heap from a set ofnitems, most of the items are in positions that will eventually hold leaves of thed-ary tree, and no downward swapping is performed for those items. At mostn/d+ 1items are non-leaves, and may be swapped downwards at least once, at a cost ofO(d)time to find the child to swap them with. At mostn/d2+ 1nodes may be swapped downward two times, incurring an additionalO(d)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

[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:

,[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

,[8]

for d = 2, and to

,[8]

for d = 3.

The space usage of thed-aryheap, 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[edit]

When operating on agraphwithmedges andnvertices, bothDijkstra's algorithmforshortest pathsandPrim's algorithmforminimum spanning treesuse a min-heap in which there arendelete-min operations and as many asmdecrease-priority operations. By using ad-ary heap withd=m/n,the total times for these two types of operations may be balanced against each other, leading to a total time ofO(mlogm/nn)for the algorithm, an improvement over theO(mlogn)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, theFibonacci heap,gives an even better theoretical running time ofO(m+nlogn),but in practiced-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, ad-ary heap typically runs much faster than a binary heap for heap sizes that exceed the size of the computer'scache memory: A binary heap typically requires morecache missesandvirtual memorypage faultsthan ad-ary heap, each one taking far more time than the extra work incurred by the additional comparisons ad-ary heap makes compared to a binary heap.[6][10]

References[edit]

  1. ^abcdJohnson, D. B.(1975), "Priority queues with update and finding minimum spanning trees",Information Processing Letters,4(3): 53–57,doi:10.1016/0020-0190(75)90001-0.
  2. ^abcdefghijklTarjan, R. E.(1983), "3.2.d-heaps ",Data Structures and Network Algorithms,CBMS-NSF Regional Conference Series in Applied Mathematics, vol. 44,Society for Industrial and Applied Mathematics,pp. 34–38.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. ^abcdefghWeiss, M. A. (2007), "d-heaps ",Data Structures and Algorithm Analysis(2nd ed.), Addison-Wesley, p. 216,ISBN978-0-321-37013-6.
  4. ^Jensen, C.; Katajainen, J.; Vitale, F. (2004),An extended truth about heaps(PDF),archived fromthe original(PDF)on 2007-06-09,retrieved2008-02-05.
  5. ^abTarjan (1983),pp. 77 and 91.
  6. ^abNaor, D.; Martel, C. U.; Matloff, N. S. (October 1991), "Performance of priority queue structures in a virtual memory environment",Computer Journal,34(5): 428–437,doi:10.1093/comjnl/34.5.428.
  7. ^abMortensen, C. W.; Pettie, S. (2005), "The complexity of implicit and space efficient priority queues",Algorithms and Data Structures: 9th International Workshop, WADS 2005, Waterloo, Canada, August 15–17, 2005, Proceedings,Lecture Notes in Computer Science, vol. 3608, Springer-Verlag, pp. 49–60,doi:10.1007/11534273_6,ISBN978-3-540-28101-6.
  8. ^abcSuchenek, 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.
  9. ^Cherkassky, Boris V.;Goldberg, Andrew V.;Radzik, Tomasz (May 1996),"Shortest paths algorithms: Theory and experimental evaluation",Mathematical Programming,73(2): 129–174,CiteSeerX10.1.1.48.752,doi:10.1007/BF02592101.
  10. ^Kamp, Poul-Henning (11 June 2010),"You're doing it wrong",ACM Queue,8(6).

External links[edit]