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

A monotone priority queue of integer keys, beyond the book: the radix heap of Ahuja, Mehlhorn, Orlin and Tarjan, in the form with digits of \( B \) bits of their two-level heap. More...

#include "ADS/Contract.h"
#include "ADS/Modification.h"
#include "ADS/memory/AllocatorAware.h"
#include <array>
#include <bit>
#include <concepts>
#include <cstddef>
#include <cstdint>
#include <limits>
#include <type_traits>
#include <utility>
#include <vector>
#include "ADS/priority/impl/RadixHeap.tpp"

Go to the source code of this file.

Classes

class  ADS::RadixHeap< T, K, B, A, Policy >
 a monotone queue of items with integer keys of the type K, the least key first, in buckets by digits of \( B \) bits More...

Namespaces

namespace  ADS

Detailed description

A monotone priority queue of integer keys, beyond the book: the radix heap of Ahuja, Mehlhorn, Orlin and Tarjan, in the form with digits of \( B \) bits of their two-level heap.

It serves the algorithms whose extracted keys never decrease, i.e., Dijkstra's algorithm with nonnegative integer weights and the event simulations, where a key pushed is never smaller than the last one extracted, \( \ell \).

A key \( x \) is a word of \( W \) bits (a signed key has its sign bit flipped, which keeps the order), split into \( \lceil W / B \rceil \) digits of \( B \) bits. Let \( r \) be the digit of the highest bit in which \( x \) and \( \ell \) differ (\( r = 0 \) if \( x = \ell \)), and \( x_r \) the digit \( r \) of \( x \): \( x \) lies in the bucket \( ( r , x_r ) \), of index \( 2^B r + x_r \). Since \( x \geq \ell \) and the two agree above the digit \( r \), every key of a bucket is smaller than every key of a bucket of larger index, and the keys of a bucket of the row 0 are all equal to \( \ell \) but for the last digit, i.e., they are equal. Hence the minimum is in the first bucket that is not empty, found by the lowest bit of a mask of the buckets in use; if this bucket is in the row \( r > 0 \), its least key, kept as the items are pushed, becomes the new \( \ell \) and its items are moved to their buckets for it, all in rows below \( r \). The other buckets keep their items: a key of the row \( r' \geq r \) agrees with the new \( \ell \) in the same digits as with the old one.

Each item thus moves at most \( \lceil W / B \rceil - 1 \) times, so that, with the potential \( \Phi = \sum_x \mathrm{row}( x ) \) and every item pushed also extracted, push() takes \( O( 1 ) \) time and pop() \( O( W / B + 2^B W / ( B w ) ) \) amortized, the second term the scan of the mask of \( 2^B \lceil W / B \rceil \) bits in words of \( w = 64 \). A key of a band of width \( C \) above \( \ell \) may lie in a high row, since a carry changes high bits, but after its first move its row is at most \( \lceil \log_2 C \rceil / B \), so that it moves at most \( 1 + \lceil \log_2 C \rceil / B \) times. With \( B = 1 \) it is the radix heap of the paper, \( O( \log C + W / w ) \) amortized per pop(); larger digits trade fewer moves for more buckets, as in the two-level heap of the paper. The buckets are vectors that keep their memory, so that a queue in a steady state calls no allocator, and the items of a bucket come out last pushed first.

The buckets come from the allocator A, std::allocator by default, with the rules of the standard [see AllocatorAware.h].

Between two episodes of a search, rebase( k ) lets the keys pushed next start from \( k \), below the last one extracted, provided that no key in the heap is below \( k \): the items are placed again for the new \( \ell = k \), in \( O( n + 2^B W / B ) \) time (no move at all when the heap is empty). extract( theta , out ) moves to out every item of key at most \( \theta \), in increasing order of the keys, reading the least key by peekKey(), so that a key above \( \theta \) is not taken for extracted and the keys pushed next need only be not smaller than the last one moved (the stepping algorithms, which extract a band of keys and then push the keys that its relaxations give).

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(), after it, and Erased by pop(), before the removal, and by extractBelow() and extract(), as each item is extracted, both with the item as the handle and its key; Cleared after clear(), and Rebuilt at the end of a mute. A heap has no handles, and the same item may be in it several times (the lazy decrease of section 10.1, an item pushed again when its key improves): a batch then coalesces the events of an item by its key as well, an erasure cancelling an insertion of the same item and key [see coalesce_traits], so that the net events are those of a multiset of pairs. rebase() changes neither the items nor their keys, and tells nothing.

The contracts [see Contract.h]: a key pushed is not smaller than any key extracted, by pop() or by extractBelow(), or read by top() and topKey() (any key before the first of them and after clear()), which is checked against the largest of those keys, kept apart from \( \ell \) since extractBelow() may extract keys above it, and top(), topKey(), peekKey() and pop() need a queue that is not empty; the invariant, checked on queues of at most 1024 items, is that each item is in its bucket and that the mask and the least keys of the buckets are right. The operations are defined in RadixHeap.tpp.

See also
R. K. Ahuja, K. Mehlhorn, J. Orlin, R. E. Tarjan, Faster algorithms for the shortest path problem, Journal of the ACM 37(2), 213-223, 1990, doi:10.1145/77600.77615
B. V. Cherkassky, A. V. Goldberg, T. Radzik, Shortest paths algorithms: theory and experimental evaluation, Mathematical Programming 73(2), 129-174, 1996, doi:10.1007/BF02592101
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa