|
ADS
Algorithms and Data Structures
|
A \( D \)-ary heap of the integers \( [ 0 , n ) \) with keys and decrease-key, beyond the book: the heap of DaryHeap [see DaryHeap.h], whose items are the pairs \( ( \mathit{key} , \mathit{id} ) \) with the key inline, so that a comparison never leaves the heap, and a vector of the places of the ids, which takes the place of the handles of HeapPriorityQueue [see HeapPriorityQueue.h]. More...
#include "ADS/Contract.h"#include "ADS/Modification.h"#include "ADS/memory/AllocatorAware.h"#include "ADS/Portable.h"#include <algorithm>#include <cstddef>#include <type_traits>#include <utility>#include <vector>#include "ADS/priority/impl/IndexedDaryHeap.tpp"Go to the source code of this file.
Classes | |
| class | ADS::IndexedDaryHeap< K, D, Policy, A > |
| a \( D \)-ary heap of the ids \( [ 0 , n ) \) by their keys, the least on top, with decrease-key, its changes told through Policy More... | |
| struct | ADS::IndexedDaryHeap< K, D, Policy, A >::Entry |
| an item: an id with its key More... | |
| class | ADS::TableHeap< T, D > |
| the ids \( [ 0 , n ) \) in a \( D \)-ary heap by the keys that the table T holds for them, read from it by update( id ): the queue of the frontier as an observer of the table of a search, in the form of the LaB-PQ of Dong et al. More... | |
Namespaces | |
| namespace | ADS |
A \( D \)-ary heap of the integers \( [ 0 , n ) \) with keys and decrease-key, beyond the book: the heap of DaryHeap [see DaryHeap.h], whose items are the pairs \( ( \mathit{key} , \mathit{id} ) \) with the key inline, so that a comparison never leaves the heap, and a vector of the places of the ids, which takes the place of the handles of HeapPriorityQueue [see HeapPriorityQueue.h].
The children of the place \( i \) are \( D i + 1 , \ldots , D i + D \) and its parent \( \lfloor ( i - 1 ) / D \rfloor \), so that a heap of \( s \) items has \( \lceil \log_D ( ( D - 1 ) s + 1 ) \rceil \) levels; push() and decrease() move up at most that many levels, one comparison each, and pop() moves down as many with \( D \) comparisons each:
\[ T_{\mathrm{push}} , T_{\mathrm{decrease}} = O( \log_D s ) , \qquad T_{\mathrm{pop}} = O( D \log_D s ) , \]
which for Dijkstra's and Prim's algorithms, \( m \) decreases and \( n \) pops, gives \( O( m \log_D n + n D \log_D n ) \), least near \( D = m / n \) (Johnson). With \( D = 4 \) a pop() reads the four children of a node in half a line of cache when the keys are 32-bit integers, the choice that Larkin, Sen and Tarjan found the fastest in practice; the space is \( ( 4 + k ) s + 4 n \) bytes, \( k \) the size of a key. It is a PRIORITYQUEUE of section 10.1 [see PriorityQueue.h], the ids its items and their own handles, so that the algorithms of the book that take a priority queue run on it [see ShortestPaths.h, MinimumSpanningTree.h].
Beyond the PRIORITYQUEUE, the heap is addressable in full: increase() raises the key of an id, moving it down as pop() moves the last item, \( O( D \log_D s ) \), update() sets it either way, and erase() removes an id from anywhere, the last item taking its place and moving up or down from there, \( O( D \log_D s ) \); these are what a search needs to take a node out of its frontier or to make it worse, e.g., when a bound prunes it (see Heaps; its events are in The events of the indexed heap).
The range of the ids is set by the constructor and by reset( n ), which empties the heap, and raised by grow( n ), which keeps it, in \( O( 1 ) \) amortized time per new id.
Two operations serve the searches that take or recompute many keys at once: extract( theta , out ) removes every id of key at most \( \theta \) (the stepping algorithms), and rekey( f ) sets the key of every id to \( f( \mathit{id} ) \) and makes the order again from the bottom in \( O( s ) \) (a change of the function of the keys, as the \( \epsilon \) of AD*). TableHeap, below, keeps the keys in the table of a search and reads them from it.
The heap tells its changes through the subject policy Policy [see Modification.h], NoObservers by default, with which every operation compiles to the code of the plain heap: Inserted by push(), Erased by erase() and pop() (the id and its key, before the removal), KeyChanged by decrease(), increase() and update() (the old and the new key), Cleared after clear() and reset(), Rebuilt after rekey() and at the end of a mute, and Relocated on demand, only if an observer wants it, for each step of the item that an operation moves: the item from the place from to the place to and the item at to, if any, to from, an exchange, so that an observer that applies them in their order to a copy of the vector of the heap keeps it equal to the vector. The event of the item comes first (the new item at the place \( s \), the last one, before its moves; the key changed in its place; the place of the item erased left empty, the last item moved into it by the first Relocated). The ids are the handles of the events, stable while an id is in the heap, and the places of the vector, which are internal maintenance, travel only on Relocated. A heap of keys has no shift of all its keys in \( O( 1 ) \), hence no Shifted.
The vectors come from the allocator A, std::allocator by default, given to the constructor that takes std::allocator_arg first, with the rules of the standard [see AllocatorAware.h].
The contracts [see Contract.h]: the ids are in \( [ 0 , n ) \), push() needs an id not in the heap, decrease() one in the heap and a key not greater than its own, increase() one in the heap and a key not less than its own, update() and erase() one in the heap, top() and pop() a heap that is not empty; the invariant is the order of the heap, no key less than that of the parent, with the places of the ids in their vector. The operations are defined in IndexedDaryHeap.tpp.