|
ADS
Algorithms and Data Structures
|
The PRIORITYQUEUE [see PriorityQueue.h] on a pairing heap, beyond the book: the self-adjusting heap of Fredman, Sedgewick, Sleator and Tarjan, the simplest of the heaps with decrease-key and, in the experiments of Stasko and Vitter and of Larkin, Sen and Tarjan, the fastest one with pointers in practice. More...
#include "ADS/Capabilities.h"#include "ADS/Contract.h"#include "ADS/memory/AllocatorAware.h"#include "ADS/memory/ObjectPool.h"#include "ADS/priority/PriorityQueue.h"#include <concepts>#include <cstddef>#include <type_traits>#include <utility>#include <vector>#include "ADS/priority/impl/PairingHeap.tpp"Go to the source code of this file.
Classes | |
| class | ADS::PairingHeap< T, P, Aux, A > |
| a priority queue on a pairing heap, two-pass, or auxiliary two-pass if Aux More... | |
| struct | ADS::PairingHeap< T, P, Aux, A >::Node |
| a node of the heap More... | |
Namespaces | |
| namespace | ADS |
Variables | |
| template<class T, std::totally_ordered P, bool Aux, class A> | |
| constexpr bool | ADS::enable_stable_references< PairingHeap< T, P, Aux, A > > |
| the items of a pairing heap stay in their nodes, which the handles point to [see Capabilities.h] | |
The PRIORITYQUEUE [see PriorityQueue.h] on a pairing heap, beyond the book: the self-adjusting heap of Fredman, Sedgewick, Sleator and Tarjan, the simplest of the heaps with decrease-key and, in the experiments of Stasko and Vitter and of Larkin, Sen and Tarjan, the fastest one with pointers in practice.
The heap is one tree, each node not smaller than its parent, kept in the form child-sibling: a node points to its leftmost child and to its right sibling, and back to its left sibling, or to its parent if it is the leftmost child, so that a node is cut from its tree in \( O( 1 ) \). The linking of two trees makes the root of larger key the leftmost child of the other one. insert() links a tree of one node with the root; decrease() cuts the node with its subtree, unless it is the leftmost child and still not smaller than its parent, and links it with the root; deleteMin() removes the root and joins its children by two passes: the first links them in pairs from the left, the second links the results from the right into one tree. The amortized bounds known for the two passes are \( O( \log n ) \) for deleteMin() and \( O( 2^{2 \sqrt{\log \log n}} ) \) for insert() and decrease() (Pettie; \( O( 1 ) \) for insert(), Iacono), while Fredman proved that \( \Omega( \log \log n ) \) is needed by decrease() in this family of heaps, against the \( O( 1 ) \) of the Fibonacci heap [see FibonacciHeapPriorityQueue.h], whose constants are larger.
With Aux true the heap is the auxiliary two-pass variant of Stasko and Vitter: the trees of insert() and decrease() go to a list beside the root, with a pointer to the least of the roots, so that each costs one comparison, and deleteMin() first joins the list by the multipass pairing (the trees linked in pairs from the front of a queue, the result to its back, until one is left) and links it with the root; it pays when the inserts outnumber the calls of deleteMin().
The nodes come from an ObjectPool [see ObjectPool.h], which takes back the node of deleteMin() for the next insert(), so that a node costs no call to the allocator. The two passes run in place on the sibling pointers, with no stack and no vector.
Beyond the PRIORITYQUEUE, erase() removes the item of any handle, joining the children of its node by two passes in its place, and increase() raises a priority by taking the node out in the same way and inserting it again, alone, so that its handle stays valid; both cost as a deleteMin() on the subtree of the node, \( O( \log n ) \) amortized, and update() takes decrease() or increase() as the new priority asks.
The nodes come from an ObjectPool [see ObjectPool.h] whose blocks come from the allocator A, HugePageAllocator by default; the heap moves with its pool and its allocator, and is never copied.
The contracts [see Contract.h]: min(), minPriority() and deleteMin() need a heap that is not empty, decrease() a handle of a node of this heap and a priority not larger than its own, increase() one not smaller, erase() and update() a handle of this heap; the invariant, checked on heaps of at most 1024 nodes, is the order of the heap, the links back and size() counting the nodes. The operations are defined in PairingHeap.tpp.