The -ary heap or -heap is a
priority queue
In computer science, a priority queue is an abstract data type similar to a regular queue (abstract data type), queue or stack (abstract data type), stack abstract data type.
In a priority queue, each element has an associated ''priority'', which ...
data structure
In computer science, a data structure is a data organization and storage format that is usually chosen for Efficiency, efficient Data access, access to data. More precisely, a data structure is a collection of data values, the relationships amo ...
, a generalization of the
binary heap
A binary heap is a heap (data structure), heap data structure that takes the form of a binary tree. Binary heaps are a common way of implementing priority queues. The binary heap was introduced by J. W. J. Williams in 1964 as a data structure fo ...
in which the nodes have children instead of 2.
Thus, a binary heap is a 2-heap, and a ternary heap is a 3-heap. According to Tarjan
and Jensen et al., -ary heaps were invented by
Donald B. Johnson in 1975.
[.]
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
Dijkstra's algorithm ( ) is an algorithm for finding the shortest paths between nodes in a weighted graph, which may represent, for example, a road network. It was conceived by computer scientist Edsger W. Dijkstra in 1956 and published three ...
in which decrease priority operations are more common than delete min operations.
Additionally, -ary heaps have better
memory cache
In computing, a cache ( ) is a hardware or software component that stores data so that future requests for that data can be served faster; the data stored in a cache might be the result of an earlier computation or a copy of data stored elsew ...
behavior than binary heaps, allowing them to run more quickly in practice despite having a theoretically larger worst-case running time.
Like binary heaps, -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.
[.]
Data structure
The -ary heap consists of an
array
An array is a systematic arrangement of similar objects, usually in rows and columns.
Things called an array include:
{{TOC right
Music
* In twelve-tone and serial composition, the presentation of simultaneous twelve-tone sets such that the ...
of items, each of which has a priority associated with it. These items may be viewed as the nodes in a complete -ary tree, listed in
breadth first traversal order: the item at position 0 of the array (using
zero-based numbering
Zero-based numbering is a way of numbering in which the initial element of a sequence is assigned the index 0, rather than the index 1 as is typical in everyday non-mathematical or non-programming circumstances. Under zero-based number ...
) forms the root of the tree, the items at positions 1 through are its children, the next items are its grandchildren, etc. Thus, the parent of the item at position (for any ) is the item at position and its children are the items at positions through . 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.
[. 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.]
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.
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.
To create a new heap from an array of items, one may loop over the items in reverse order, starting from the item at position and ending at the item at position 0, applying the downward-swapping procedure for each item.
Analysis
In a -ary heap containing items, both the upward-swapping procedure and the downward-swapping procedure may perform as many as 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 . In the downward-swapping procedure, each swap involves comparisons and takes time: it takes 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 .
When creating a -ary heap from a set of ''n'' items, most of the items are in positions that will eventually hold leaves of the -ary tree, and no downward swapping is performed for those items. At most items are non-leaves, and may be swapped downwards at least once, at a cost of time to find the child to swap them with. At most nodes may be swapped downward two times, incurring an additional 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
:
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:
:
,
[.]
where s
d(n) is the sum of all digits of the standard base-d representation of n and e
d(n) is the exponent of d in the factorization of n.
This reduces to
:
,
for d = 2, and to
:
,
for d = 3.
The space usage of the 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.
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.
Applications
When operating on a
graph
Graph may refer to:
Mathematics
*Graph (discrete mathematics), a structure made of vertices and edges
**Graph theory, the study of such graphs and their properties
*Graph (topology), a topological space resembling a graph in the sense of discret ...
with edges and vertices, both
Dijkstra's algorithm
Dijkstra's algorithm ( ) is an algorithm for finding the shortest paths between nodes in a weighted graph, which may represent, for example, a road network. It was conceived by computer scientist Edsger W. Dijkstra in 1956 and published three ...
for
shortest path
In graph theory, the shortest path problem is the problem of finding a path between two vertices (or nodes) in a graph such that the sum of the weights of its constituent edges is minimized.
The problem of finding the shortest path between two ...
s and
Prim's algorithm
In computer science, Prim's algorithm is a greedy algorithm that finds a minimum spanning tree for a Weighted graph, weighted undirected graph. This means it finds a subset of the edge (graph theory), edges that forms a Tree (graph theory), tree ...
for
minimum spanning tree
A minimum spanning tree (MST) or minimum weight spanning tree is a subset of the edges of a connected, edge-weighted undirected graph that connects all the vertices together, without any cycles and with the minimum possible total edge weight. ...
s use a min-heap in which there are delete-min operations and as many as decrease-priority operations. By using a -ary heap with , the total times for these two types of operations may be balanced against each other, leading to a total time of for the algorithm, an improvement over the running time of binary heap versions of these algorithms whenever the number of edges is significantly larger than the number of vertices.
[, pp. 77 and 91.] An alternative priority queue data structure, the
Fibonacci heap, gives an even better theoretical running time of , but in practice -ary heaps are generally at least as fast, and often faster, than Fibonacci heaps for this application.
4-heaps may perform better than binary heaps in practice, even for delete-min operations.
[.] Additionally,
a -ary heap typically runs much faster than a binary heap for heap sizes that exceed the size of the computer's
cache memory
In computing, a cache ( ) is a hardware or software component that stores data so that future requests for that data can be served faster; the data stored in a cache might be the result of an earlier computation or a copy of data stored elsew ...
;
this may be due to the binary heap implicating more
cache miss
In computing, a cache ( ) is a hardware or software component that stores data so that future requests for that data can be served faster; the data stored in a cache might be the result of an earlier computation or a copy of data stored elsew ...
es or
virtual memory
In computing, virtual memory, or virtual storage, is a memory management technique that provides an "idealized abstraction of the storage resources that are actually available on a given machine" which "creates the illusion to users of a ver ...
page fault
In computing, a page fault is an exception that the memory management unit (MMU) raises when a process accesses a memory page without proper preparations. Accessing the page requires a mapping to be added to the process's virtual address space ...
s, which can consume more processing time than the extra work of the few additional comparisons at each level entailed by a -ary heap.
[.][.]
References
External links
C++ implementation of generalized heap with D-Heap support
{{DEFAULTSORT:D-Ary Heap
Heaps (data structures)