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

A priority queue for small nonnegative integer keys, beyond the book: a two-level bucket queue, whose outer level is a vector indexed by the first key \( f \) and whose inner one, for each \( f \), a set of LIFO lists, one per second key \( g \). More...

#include "ADS/Contract.h"
#include "ADS/Modification.h"
#include <algorithm>
#include <concepts>
#include <cstddef>
#include <cstdint>
#include <iterator>
#include <stdexcept>
#include <type_traits>
#include <utility>
#include <vector>
#include "ADS/priority/impl/BucketQueue.tpp"

Go to the source code of this file.

Classes

struct  ADS::LargestSecondKey
 the order of BucketQueue among the items of one \( f \): the largest \( g \) first (A*, toward the deeper nodes), the default More...
struct  ADS::LeastSecondKey
 the order of BucketQueue among the items of one \( f \): the least \( g \) first, i.e., the lexicographic order of the pairs \( ( f , g ) \) (the keys of LPA*) More...
class  ADS::BucketQueue< T, K, Tie, Policy >
 a two-level bucket queue: least \( f \) first, then largest (or, by the policy Tie, least) \( g \), then LIFO More...
struct  ADS::BucketQueue< T, K, Tie, Policy >::Entry
 an item with its keys More...
struct  ADS::BucketQueue< T, K, Tie, Policy >::TopEntry
 the item of top() with its keys, read in place More...
class  ADS::BucketQueue< T, K, Tie, Policy >::const_iterator
class  ADS::BucketHeap< E, C >
 a BucketQueue behind the interface of a heap of pairs \( ( k , x ) \), the key \( k \) of packFG(): the inner queue of a MultiQueue over such keys [see MultiQueue.h], whose pushes and pops take \( O( 1 ) \) amortized time with bounded keys where those of a heap take \( O( \log n ) \). More...

Namespaces

namespace  ADS

Concepts

concept  ADS::BucketTie
 a policy of the ties of BucketQueue: whether the least \( g \) comes first

Functions

constexpr std::uint64_t ADS::packFG (std::uint32_t f, std::uint32_t g) noexcept
 the key of 64 bits of the pair \( ( f , g ) \), \( f \) in the high half and the complement of \( g \) in the low one, so that the keys under std::less come by least \( f \) and then largest \( g \), the order of a BucketQueue
constexpr std::uint32_t ADS::keyF (std::uint64_t k) noexcept
 the \( f \) of a key of packFG()
constexpr std::uint32_t ADS::keyG (std::uint64_t k) noexcept
 the \( g \) of a key of packFG()

Detailed description

A priority queue for small nonnegative integer keys, beyond the book: a two-level bucket queue, whose outer level is a vector indexed by the first key \( f \) and whose inner one, for each \( f \), a set of LIFO lists, one per second key \( g \).

top() is an item of least \( f \) and, among those, of largest \( g \), the last one pushed (the order that A* wants: least \( f \), ties broken toward the deeper nodes). The least \( f \) is found by a scan that only moves forward between two pushes of a smaller \( f \), so that the outer level costs \( O( 1 ) \) amortized per operation when the keys are bounded, against the \( O( \log n ) \) of a heap: the queue of Dial for the shortest paths with integer weights, nested as in the search code of Burns et al.

The order among the items of one \( f \) is a policy, the third parameter: LargestSecondKey, the default, is the one above, while LeastSecondKey gives the item of least \( g \) first, and among those the last pushed, i.e., the lexicographic order of the pairs \( ( f , g ) \), the keys \( ( k_1 , k_2 ) \) of LPA* and D* Lite. The two are mirror images: with the least \( g \) first the bound of the dense lists is scanned forward instead of backward, the runs are kept by decreasing \( g \), so that their last one is still the least, and a layer gives its dense lists, all of \( g < D \) while it has runs, before its runs. The default compiles to the same code as before the policy existed.

A layer (the items of one \( f \)) keeps the heads of its lists in two ways. The lists of \( g < D \) (\( D = 256 \)) are dense: an array indexed by \( g \), the first \( S = 4 \) heads in the layer itself and the others in a vector, whose largest \( g \) with items is found by a scan backward from a bound that only moves down between two pushes of a larger \( g \). The lists of \( g \ge D \) are sparse while they are at most \( R = 64 \): a vector of runs \( ( g , h ) \), sorted by increasing \( g \), whose last run is the top of the layer. A push of any \( g \ge D \), new or not, into a layer of \( R \) runs turns it dense on all its \( g \), moving the heads of the runs, not the items, into the vector, and the layer stays dense until it empties, when it frees its vector and is sparse again; the vectors of runs are kept, empty, for the next layer that needs one. A layer filled with \( m \) items and then emptied costs \( O( m + D ) \) if no push of a \( g \ge D \) meets \( R \) runs, and \( O( m + G_f ) \) otherwise, \( G_f \) the largest \( g \) of the layer \( f \), paid by more than \( R \) pushes, where a dense array alone would cost \( \Theta( G_f ) \) at each filling, even of one item; with the pushes and the pops interleaved, each push that raises the bound of the scan may cost up to \( D \) (or \( G_f \)) steps of the scan at a later pop. The argument and the memory, \( O( n + F + \sum_f H_f + R L ) \) words, are in the theory of the area (see Monotone queues of integer keys).

The items lie in one vector of cells, each with the index of the next cell of its list, and the cells freed by pop() form a list of their own that push() takes from first, so that a push or a pop costs no call to the allocator once the vector has grown to the largest size of the queue. There is no decrease-key: as in the lazy deletion of A*, an item whose key improves is pushed again, and the caller skips the stale copies when they come out.

The cells and the second keys of the dense lists are numbered by 32 bits: a push that would hold more than \( 2^{32} - 1 \) items at once, or give a dense layer a second key past \( 2^{32} - 2 \), throws std::length_error and leaves the items of the queue as they were.

extract( theta , out ) moves to out the items of first key at most \( \theta \), in the order of pop(), as the bulk-synchronous searches take a band of the least \( f \) at once.

The queue 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 queue: Inserted by push(), after it, and Erased by pop(), popEntry() and extract(), before the removal, both with the item as the handle, \( f \) as the key and \( g \) as the value; Cleared after clear(), and Rebuilt at the end of a mute. The same item may be in the queue several times (the stale copies above), and a batch coalesces its events by its keys as well [see RadixHeap.h].

The contracts [see Contract.h]: the keys are nonnegative, and top(), topF(), topG() and pop() need a queue that is not empty; the invariant is that the counts of the layers add up to size(), and that the runs of the layer changed are at most \( R \), of \( g \ge D \), increasing (decreasing with LeastSecondKey) and with items, and none in a dense layer. The operations are defined in BucketQueue.tpp.

See also
R. B. Dial, Algorithm 360: shortest-path forest with topological ordering, Communications of the ACM 12(11), 632-633, 1969, doi:10.1145/363269.363610
E. Burns, M. Hatem, M. J. Leighton, W. Ruml, Implementing fast heuristic search code, Proceedings of the 5th Symposium on Combinatorial Search (SoCS), 25-32, 2012, doi:10.1609/socs.v3i1.18245
S. Koenig, M. Likhachev, D. Furcy, Lifelong Planning A*, Artificial Intelligence 155(1-2), 93-146, 2004, doi:10.1016/j.artint.2003.12.001
Author
Donato Meoli
Dipartimento di Informatica
Università di Pisa