ADS
Algorithms and Data Structures
Loading...
Searching...
No Matches
DaryHeap.h File Reference

A \( D \)-ary heap of plain items, beyond the book: the heap of section 10.1 with \( D \) children per node in a vector, the children of the node \( i \) in the places \( D i + 1 , \ldots , D i + D \) [see BinaryTree.h], ordered by a predicate better( a , b ), true if \( a \) must come out before \( b \). More...

#include "ADS/Contract.h"
#include "ADS/memory/AllocatorAware.h"
#include "ADS/memory/HugePageAllocator.h"
#include <algorithm>
#include <cstddef>
#include <functional>
#include <iterator>
#include <utility>
#include <vector>
#include "ADS/priority/impl/DaryHeap.tpp"

Go to the source code of this file.

Classes

class  ADS::DaryHeap< T, Better, D, BottomUp, A >
 a \( D \)-ary heap of plain items, the best one by better() on top; pop() by the walk down from the top, or from the bottom up if BottomUp More...

Namespaces

namespace  ADS

Detailed description

A \( D \)-ary heap of plain items, beyond the book: the heap of section 10.1 with \( D \) children per node in a vector, the children of the node \( i \) in the places \( D i + 1 , \ldots , D i + D \) [see BinaryTree.h], ordered by a predicate better( a , b ), true if \( a \) must come out before \( b \).

The items are meant to be small and to carry their keys (e.g., \( f \), \( g \) and a pointer to the node of a search), so that a comparison never follows a pointer; with \( D = 4 \) the heap is half as deep as a binary one, the four children of a node are consecutive (with the root at 0 they start at \( 4 i + 1 \), and 16-byte items straddle two lines of 64 bytes, since the array is not shifted to align them), and a push moves up half as many levels, at the price of \( D - 2 \) more comparisons per level on the way down (the same in all, \( 4 \log_4 n = 2 \log_2 n \)). With BottomUp, pop() moves the hole of the root down to a leaf by the best children, \( D - 1 \) comparisons per level and none with the item that fills it, and then moves the last item up from there (the bottom-up heapsort of Floyd and Wegener): it pays when the last item, as in a heap full of stale copies, would go down to the bottom anyway, and loses when it stops halfway, as in the hold model of the simulations, where the default walk from the top stops as soon as the item fits.

There are no handles and no decrease-key: an item whose key improves is pushed again and the stale copy skipped (lazy deletion), which is what makes the items plain and the heap fast. The vector of a large heap is on huge pages [see HugePageAllocator.h], so that the walk from the root to a leaf does not miss the TLB at each level and the vector, when it grows, is faulted in 2 MB at a time; another allocator A (e.g., one of std::pmr) may give the vector instead, with the rules of the standard [see AllocatorAware.h]. HeapPriorityQueue [see HeapPriorityQueue.h] is the realization with handles of the book.

With \( D = 2 \) it is the BinaryHeap of section 10.1 of Morin's book, whose array in the order of Eytzinger is the general one of its exercise 10.4 [see parent() and child()], and for any \( D \) it is the DaryHeap of its exercise 10.5: a push moves up \( \log_D n \) levels with one comparison each, a pop moves down as many with \( D \) comparisons each, so that \( D = 4 \) halves the levels of both and doubles the comparisons per level of a pop, which leaves those of a pop the same in all and is the better trade on the machines with caches. Beyond the book, erase( i ) removes the item in a place (exercise 10.3), secondTop() gives the second best item (exercise 10.9) and smallest( k ) the \( k \) best (exercise 10.10). Against the code of Morin's book: the hole moves instead of the item being swapped at each level, one write per level instead of the three of a swap, and the walk down compares the children with each other and the best with the item, \( D \) comparisons per level as in the book (where it is meant to be faster); the vector is not shrunk when it is a third full, as the book does, so that a heap keeps the memory of its largest size and never copies its items for it; and the vector of a large heap is on huge pages.

The contracts [see Contract.h]: \( D \ge 2 \), top() and pop() need a heap that is not empty; the invariant is the order of the heap, no item better than its parent; erase( i ) needs \( i < n \). The operations are defined in DaryHeap.tpp.

See also
A. Montresor, Algoritmi e Strutture Dati, 2025, section 10.1, https://algoritmicamente.it/materiali/libro/capitoli/cap10.pdf
P. Morin, Open Data Structures (in C++), Edition 0.1G, 2013, https://opendatastructures.org/ods-cpp.pdf, section 10.1 and exercises 10.3 to 10.5, 10.9 and 10.10
J. W. J. Williams, Algorithm 232: Heapsort, Communications of the ACM 7(6), 347-348, 1964, doi:10.1145/512274.3734138
D. B. Johnson, Priority queues with update and finding minimum spanning trees, Information Processing Letters 4(3), 53-57, 1975, doi:10.1016/0020-0190(75)90001-0
R. W. Floyd, Algorithm 245: Treesort, Communications of the ACM 7(12), 701, 1964, doi:10.1145/355588.365103
I. Wegener, BOTTOM-UP-HEAPSORT, a new variant of HEAPSORT beating, on an average, QUICKSORT (if n is not very small), Theoretical Computer Science 118(1), 81-98, 1993, doi:10.1016/0304-3975(93)90364-Y
A. LaMarca, R. E. Ladner, The influence of caches on the performance of heaps, ACM Journal of Experimental Algorithmics 1, article 4, 1996, doi:10.1145/235141.235145
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa