A bucket queue is a
data structure
In computer science, a data structure is a data organization and storage format that is usually chosen for Efficiency, efficient Data access, access to data. More precisely, a data structure is a collection of data values, the relationships amo ...
that implements the
priority queue
In computer science, a priority queue is an abstract data type similar to a regular queue (abstract data type), queue or stack (abstract data type), stack abstract data type.
In a priority queue, each element has an associated ''priority'', which ...
abstract data type
In computer science, an abstract data type (ADT) is a mathematical model for data types, defined by its behavior (semantics) from the point of view of a '' user'' of the data, specifically in terms of possible values, possible operations on data ...
: it maintains a dynamic collection of elements with numerical priorities and allows quick access to the element with minimum (or maximum) priority. In the bucket queue, the priorities must be
integer
An integer is the number zero (0), a positive natural number (1, 2, 3, ...), or the negation of a positive natural number (−1, −2, −3, ...). The negations or additive inverses of the positive natural numbers are referred to as negative in ...
s, and it is particularly suited to applications in which the priorities have a small range. A bucket queue has the form of an array of buckets: an
array data structure
In computer science, an array is a data structure consisting of a collection of ''elements'' (value (computer science), values or variable (programming), variables), of same memory size, each identified by at least one ''array index'' or ''key' ...
, indexed by the priorities, whose cells contain
collections of items with the same priority as each other. With this data structure, insertion of elements and changes of their priority take
constant time
In theoretical computer science, the time complexity is the computational complexity that describes the amount of computer time it takes to run an algorithm. Time complexity is commonly estimated by counting the number of elementary operations p ...
. Searching for and removing the minimum-priority element takes time proportional to the number of buckets or, by maintaining a pointer to the most recently found bucket, in time proportional to the difference in priorities between successive operations.
The bucket queue is the priority-queue analogue of
pigeonhole sort (also called bucket sort), a sorting algorithm that places elements into buckets indexed by their priorities and then concatenates the buckets. Using a bucket queue as the priority queue in a
selection sort gives a form of the pigeonhole sort algorithm.
[ Bucket queues are also called bucket priority queues][ or bounded-height priority queues.] When used for quantized approximations to real number
In mathematics, a real number is a number that can be used to measure a continuous one- dimensional quantity such as a duration or temperature. Here, ''continuous'' means that pairs of values can have arbitrarily small differences. Every re ...
priorities, they are also called untidy priority queues or pseudo priority queues.[ They are closely related to the calendar queue, a structure that uses a similar array of buckets for exact prioritization by real numbers.
Applications of the bucket queue include computation of the degeneracy of a graph, fast ]algorithm
In mathematics and computer science, an algorithm () is a finite sequence of Rigour#Mathematics, mathematically rigorous instructions, typically used to solve a class of specific Computational problem, problems or to perform a computation. Algo ...
s for shortest paths and widest paths for graphs with weights that are small integers or are already sorted, and greedy approximation algorithm
In computer science and operations research, approximation algorithms are efficient algorithms that find approximate solutions to optimization problems (in particular NP-hard problems) with provable guarantees on the distance of the returned sol ...
s for the set cover problem
The set cover problem is a classical question in combinatorics, computer science, operations research, and complexity theory.
Given a set of elements (henceforth referred to as the universe, specifying all possible elements under considerati ...
. The quantized version of the structure has also been applied to scheduling[ and to ]marching cubes
Marching cubes is a computer graphics algorithm, published in the 1987 SIGGRAPH proceedings by Lorensen and Cline, for extracting a polygonal mesh of an isosurface from a three-dimensional discrete scalar field (the elements of which are somet ...
in computer graphics
Computer graphics deals with generating images and art with the aid of computers. Computer graphics is a core technology in digital photography, film, video games, digital art, cell phone and computer displays, and many specialized applications. ...
.[ The first use of the bucket queue][. See also p. 157 for the history and naming of this structure.] was in a shortest path algorithm by .[.]
Operation
Basic data structure
A bucket queue can handle elements with integer priorities in the range from 0 or 1 up to some known bound , and operations that insert elements, change the priority of elements, or extract (find and remove) the element that has the minimum (or maximum) priority. It consists of an array of container data structures; in most sources these containers are doubly linked lists but they could alternatively be dynamic array
In computer science, a dynamic array, growable array, resizable array, dynamic table, mutable array, or array list is a random access, variable-size list data structure that allows elements to be added or removed. It is supplied with standard l ...
s or dynamic sets. The container in the th array cell stores the collection of elements whose priority
A bucket queue can handle the following operations:
* To insert an element with priority , add to the container at .
* To change the priority of an element, remove it from the container for its old priority and re-insert it into the container for its new priority.
* To extract an element with the minimum or maximum priority, perform a sequential search
In computer science, linear search or sequential search is a method for finding an element within a list. It sequentially checks each element of the list until a match is found or the whole list has been searched.
A linear search runs in linea ...
in the array to find the first or last non-empty container, respectively, choose an arbitrary element from this container, and remove it from the container.
In this way, insertions and priority changes take constant time, and extracting the minimum or maximum priority element takes time .[.][.]
Optimizations
As an optimization, the data structure can start each sequential search for a non-empty bucket at the most recently-found non-empty bucket instead of at the start of the array. This can be done in either of two different ways, lazy (delaying these sequential searches until they are necessary) or eager (doing the searches ahead of time). The choice of when to do the search affects which of the data structure operations is slowed by these searches. Dial's original version of the structure used a lazy search. This can be done by maintaining an index that is a lower bound on the minimum priority of any element currently in the queue. When inserting a new element, should be updated to the minimum of its old value and the new element's priority. When searching for the minimum priority element, the search can start at instead of at zero, and after the search should be left equal to the priority that was found in the search.[ Alternatively, the eager version of this optimization keeps updated so that it always points to the first non-empty bucket. When inserting a new element with a priority smaller than , the data structure sets to the new priority, and when removing the last element from a bucket with priority , it performs a sequential search through larger indexes until finding a non-empty bucket and setting to the priority of the resulting bucket.]
In either of these two variations, each sequential search takes time proportional to the difference between the old and new values of . This could be significantly faster than the time bound for the searches in the un-optimized version of the data structure. In many applications of priority queues such as Dijkstra's algorithm, the minimum priorities form a monotonic sequence, allowing a monotone priority queue to be used. In these applications, for both the lazy and eager variations of the optimized structure, the sequential searches for non-empty buckets cover disjoint ranges of buckets. Because each bucket is in at most one of these ranges, their numbers of steps add to at most . Therefore, in these applications, the total time for a sequence of operations is , rather than the slower time bound that would result without this optimization.[ A corresponding optimization can be applied in applications where a bucket queue is used to find elements of maximum priority, but in this case it should maintain an index that upper-bounds the maximum priority, and the sequential search for a non-empty bucket should proceed downwards from this upper bound.][
Another optimization (already given by ) can be used to save space when the priorities are monotonic and, throughout the course of an algorithm, always fall within a range of values rather than extending over the whole range from 0 to . In this case, one can index the array by the priorities modulo rather than by their actual values. The search for the minimum priority element should always begin at the previous minimum, to avoid priorities that are higher than the minimum but have lower moduli. In particular, this idea can be applied in Dijkstra's algorithm on graphs whose edge lengths are integers in the range from 1 to .
Because creating a new bucket queue involves initializing an array of empty buckets, this initialization step takes time proportional to the number of priorities. A variation of the bucket queue described by Donald B. Johnson in 1981 instead stores only the non-empty buckets in a linked list, sorted by their priorities, and uses an auxiliary search tree to quickly find the position in this linked list for any new buckets. It takes time to initialize this variant structure, constant time to find an element with minimum or maximum priority, and time to insert or delete an element, where is the difference between the nearest smaller and larger priorities to the priority of the inserted or deleted element.
]
Example
For example, consider a bucket queue with four priorities, the numbers 0, 1, 2, and 3. It consists of an array whose four cells each contain a collection of elements, initially empty. For the purposes of this example, can be written as a bracketed sequence of four sets: