|
|
Line 1: |
Line 1: |
| {{DISPLAYTITLE:''d''-ary heap}}
| | Alyson is the title individuals use to contact me and I think it seems fairly great when you say it. Her family members life in Ohio. What I adore doing is soccer but I don't have the time lately. She works as a travel agent but quickly she'll be on her own.<br><br>Stop by my web-site ... psychic phone; [http://www.onbizin.co.kr/xe/?document_srl=320614 www.onbizin.co.kr], |
| The '''{{math|''d''}}-ary heap''' or '''{{math|''d''}}-heap''' is a [[priority queue]] [[data structure]], a generalization of the [[binary heap]] in which the nodes have {{math|''d''}} children instead of 2.<ref name="j75"/><ref name="t83"/><ref name="w07"/> Thus, a binary heap is a 2-heap. According to Tarjan<ref name="t83"/> and Jensen et al.,<ref>{{citation
| |
| | last1 = Jensen | first1 = C.
| |
| | last2 = Katajainen | first2 = J.
| |
| | last3 = Vitale | first3 = F.
| |
| | title = An extended truth about heaps
| |
| | url = http://www.cphstl.dk/Report/In-place-multiway-heaps/experimental-study.pdf
| |
| | year = 2004}}.</ref> {{math|''d''}}-ary heaps were invented by [[Donald B. Johnson]] in 1975.<ref name="j75">{{citation
| |
| | last = Johnson | first = D. B. | authorlink = Donald B. Johnson
| |
| | doi = 10.1016/0020-0190(75)90001-0
| |
| | journal = Information Processing Letters
| |
| | pages = 53–57
| |
| | title = Priority queues with update and finding minimum spanning trees
| |
| | volume = 4
| |
| | year = 1975
| |
| | issue = 3}}.</ref>
| |
| | |
| 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.<ref name="j75"/><ref name="t2"/> Additionally, {{math|''d''}}-ary heaps have better [[memory cache]] behavior than a binary heap, allowing them to run more quickly in practice despite having a theoretically larger worst-case running time.<ref name="nmm91"/><ref name="k10"/> Like binary heaps, {{math|''d''}}-ary heaps are an [[In-place algorithm|in-place data structure]] that uses no additional storage beyond that needed to store the array of items in the heap.<ref name="t83"/><ref name="mp05">{{citation
| |
| | last1 = Mortensen | first1 = C. W.
| |
| | last2 = Pettie | first2 = S.
| |
| | contribution = The complexity of implicit and space efficient priority queues
| |
| | doi = 10.1007/11534273_6
| |
| | pages = 49–60
| |
| | publisher = Springer-Verlag
| |
| | series = Lecture Notes in Computer Science
| |
| | title = [[SWAT and WADS conferences|Algorithms and Data Structures: 9th International Workshop, WADS 2005, Waterloo, Canada, August 15-17, 2005, Proceedings]]
| |
| | volume = 3608
| |
| | year = 2005
| |
| | isbn = 978-3-540-28101-6}}.</ref>
| |
| | |
| ==Data structure==
| |
| The {{math|''d''}}-ary heap consists of an [[Array data structure|array]] of {{math|''n''}} items, each of which has a priority associated with it. These items may be viewed as the nodes in a complete {{math|''d''}}-ary tree, listed in [[breadth-first search|breadth first traversal order]]: the item at position 0 of the array forms the root of the tree, the items at positions 1–{{math|''d''}} are its children, the next {{math|''d''<sup>2</sup>}} items are its grandchildren, etc. Thus, the parent of the item at position {{math|''i''}} (for any {{math|''i'' > 0}}) is the item at position {{math|floor((''i'' − 1)/''d'')}} and its children are the items at positions {{math|''di'' + 1}} through {{math|''di'' + ''d''}}. According to the [[binary heap|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.<ref name="t83">{{citation
| |
| | last = Tarjan | first = R. E. | author-link = Robert Tarjan
| |
| | contribution = 3.2. ''d''-heaps
| |
| | pages = 34–38
| |
| | publisher = [[Society for Industrial and Applied Mathematics]]
| |
| | series = CBMS-NSF Regional Conference Series in Applied Mathematics
| |
| | title = Data Structures and Network Algorithms
| |
| | volume = 44
| |
| | year = 1983}}.</ref><ref name="w07"/>
| |
| | |
| 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.<ref name="t83"/><ref name="w07"/>
| |
| | |
| 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.<ref name="t83"/><ref name="w07"/>
| |
| | |
| To create a new heap from an array of {{math|''n''}} items, one may loop over the items in reverse order, starting from the item at position {{math|''n'' − 1}} and ending at the item at position 0, applying the downward-swapping procedure for each item.<ref name="t83"/><ref name="w07"/>
| |
| | |
| ==Analysis==
| |
| In a {{math|''d''}}-ary heap with {{math|''n''}} items in it, both the upward-swapping procedure and the downward-swapping procedure may perform as many as {{math|1=log<sub>''d''</sub> ''n'' = log ''n'' / log ''d''}} 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 {{math|O(log ''n'' / log ''d'')}}. In the downward-swapping procedure, each swap involves {{math|''d''}} comparisons and takes {{math|O(''d'')}} time: it takes {{math|''d'' − 1}} 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 {{math|O(''d'' log ''n'' / log ''d'')}}.<ref name="t83"/><ref name="w07"/>
| |
| | |
| When creating a {{math|''d''}}-ary heap from a set of ''n'' items, most of the items are in positions that will eventually hold leaves of the {{math|''d''}}-ary tree, and no downward swapping is performed for those items. At most {{math|''n''/''d'' + 1}} items are non-leaves, and may be swapped downwards at least once, at a cost of {{math|O(''d'')}} time to find the child to swap them with. At most {{math|''n''/''d''<sup>2</sup> + 1}} nodes may be swapped downward two times, incurring an additional {{math|O(''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
| |
| :<math>\sum_{i=1}^{\log_d n} \left(\frac{n}{d^i}+1\right) O(d) = O(n).</math><ref name="t83"/><ref name="w07"/>
| |
| | |
| 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:
| |
| | |
| :<math> \frac{d}{d-1} (n - s_d (n)) - (d-1 - (n \mod d)) ( e_d ( \lfloor \frac{n}{d} \rfloor) + 1) </math>,<ref name="Suchenek">{{citation
| |
| | last1 = Suchenek | first1 = Marek A.
| |
| | title = Elementary Yet Precise Worst-Case Analysis of Floyd's Heap-Construction Program
| |
| | doi = 10.3233/FI-2012-751
| |
| | pages = 75–92
| |
| | publisher = IOS Press
| |
| | journal = Fundamenta Informaticae
| |
| | volume = 120
| |
| | issue = 1
| |
| | year = 2012
| |
| | url = http://www.deepdyve.com/lp/ios-press/elementary-yet-precise-worst-case-analysis-of-floyd-s-heap-50NW30HMxU}}.</ref>
| |
| where s<sub>d</sub>(n) is the sum of all digits of the standard base-d representation of n and e<sub>d</sub>(n) is the exponent of d in the factorization of n.
| |
| This reduces to
| |
| :<math> 2 n - 2 s_2 (n) - e_2 (n) </math>, <ref name="Suchenek" />
| |
| for d = 2, and to
| |
| :<math> \frac{3}{2} (n - s_3 (n)) - 2 e_3 (n) - e_3 (n-1) </math>,<ref name="Suchenek" />
| |
| | |
| for d = 3.
| |
| | |
| | |
| The space usage of the {{math|''d''-ary}} 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.<ref name="t83"/><ref name="mp05"/> 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.<ref name="t83"/>
| |
| | |
| ==Applications==
| |
| [[Dijkstra's algorithm]] for [[shortest path]]s in graphs and [[Prim's algorithm]] for [[minimum spanning tree]]s both use a min-heap in which there are {{math|''n''}} delete-min operations and as many as {{math|''m''}} decrease-priority operations, where {{math|''n''}} is the number of vertices in the graph and ''m'' is the number of edges. By using a {{math|''d''}}-ary heap with {{math|1=''d'' = ''m''/''n''}}, the total times for these two types of operations may be balanced against each other, leading to a total time of {{math|O(''m'' log<sub>''m''/''n''</sub> ''n'')}} for the algorithm, an improvement over the {{math|O(''m'' log ''n'')}} running time of binary heap versions of these algorithms whenever the number of edges is significantly larger than the number of vertices.<ref name="j75"/><ref name="t2">{{harvtxt|Tarjan|1983}}, pp. 77 and 91.</ref> An alternative priority queue data structure, the [[Fibonacci heap]], gives an even better theoretical running time of {{math|O(''m'' + ''n'' log ''n'')}}, but in practice {{math|''d''}}-ary heaps are generally at least as fast, and often faster, than Fibonacci heaps for this application.<ref>{{citation
| |
| | last1 = Cherkassky | first1 = B. V.
| |
| | last2 = Goldberg | first2 = A. V. | author2-link = Andrew V. Goldberg
| |
| | last3 = Radzik | first3 = T.
| |
| | doi = 10.1007/BF02592101
| |
| | issue = 2
| |
| | journal = Mathematical Programming
| |
| | pages = 129–174
| |
| | title = Shortest paths algorithms: Theory and experimental evaluation
| |
| | volume = 73
| |
| | year = 1996}}.</ref>
| |
| | |
| 4-heaps may perform better than binary heaps in practice, even for delete-min operations.<ref name="t83"/><ref name="w07">{{citation
| |
| | last = Weiss | first = M. A.
| |
| | contribution = ''d''-heaps
| |
| | edition = 2nd
| |
| | isbn = 0-321-37013-9
| |
| | page = 216
| |
| | publisher = Addison-Wesley
| |
| | title = Data Structures and Algorithm Analysis
| |
| | year = 2007}}.</ref> Additionally,
| |
| a {{math|''d''}}-ary heap typically runs much faster than a binary heap for heap sizes that exceed the size of the computer's [[cache memory]]:
| |
| A binary heap typically requires more [[cache miss]]es and [[virtual memory]] [[page fault]]s than a {{math|''d''}}-ary heap, each one taking far more time than the extra work incurred by the additional comparisons a {{math|''d''}}-ary heap makes compared to a binary heap.<ref name="nmm91">{{citation
| |
| | last1 = Naor | first1 = D.
| |
| | last2 = Martel | first2 = C. U.
| |
| | last3 = Matloff | first3 = N. S.
| |
| | year = 1991
| |
| | doi = 10.1093/comjnl/34.5.428
| |
| | issue = 5
| |
| | journal = Computer Journal
| |
| | pages = 428–437
| |
| | title = Performance of priority queue structures in a virtual memory environment
| |
| | volume = 34}}.</ref><ref name="k10">{{citation
| |
| | last = Kamp | first = Poul-Henning
| |
| | journal = ACM Queue
| |
| | title = You're doing it wrong
| |
| | url = http://queue.acm.org/detail.cfm?id=1814327
| |
| | volume = 8
| |
| | issue = 6
| |
| | year = 2010}}.</ref>
| |
| | |
| ==References==
| |
| {{reflist}}
| |
| | |
| ==External links==
| |
| *[https://github.com/valyala/gheap C++ implementation of generalized heap with D-Heap support]
| |
| | |
| {{DEFAULTSORT:D-Ary Heap}}
| |
| [[Category:Heaps (data structures)]]
| |