A bucket queue is a
data structure
In computer science, a data structure is a data organization, management, 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 rel ...
that implements the
priority queue
In computer science, a priority queue is an abstract data-type similar to a regular queue or stack data structure in which each element additionally has a ''priority'' associated with it. In a priority queue, an element with high priority is se ...
abstract data type
In computer science, an abstract data type (ADT) is a mathematical model for data types. An abstract data type is defined by its behavior (semantics) from the point of view of a '' user'', of the data, specifically in terms of possible values, po ...
: 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 (), a positive natural number (, , , etc.) or a negative integer with a minus sign ( −1, −2, −3, etc.). The negative numbers are the additive inverses of the corresponding positive numbers. In the language ...
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'' ( values or variables), each identified by at least one ''array index'' or ''key''. An array is stored such that the position of each element can be c ...
, 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 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 performed by t ...
. 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
In computer science, selection sort is an in-place comparison sorting algorithm. It has an O(''n''2) time complexity, which makes it inefficient on large lists, and generally performs worse than the similar insertion sort. Selection sort is n ...
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 measurement, measure a ''continuous'' one-dimensional quantity such as a distance, time, duration or temperature. Here, ''continuous'' means that values can have arbitrarily small var ...
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 rigorous instructions, typically used to solve a class of specific problems or to perform a computation. Algorithms are used as specifications for performing ...
s for shortest path
In graph theory, the shortest path problem is the problem of finding a path between two vertices (or nodes) in a graph such that the sum of the weights of its constituent edges is minimized.
The problem of finding the shortest path between t ...
s 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 solu ...
s for the set cover problem
The set cover problem is a classical question in combinatorics, computer science, operations research, and complexity theory. It is one of Karp's 21 NP-complete problems shown to be NP-complete in 1972.
Given a set of elements (called the un ...
. The quantized version of the structure has also been applied to scheduling
A schedule or a timetable, as a basic time-management tool, consists of a list of times at which possible tasks, events, or actions are intended to take place, or of a sequence of events in the chronological order in which such things are i ...
[ 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 someti ...
in computer graphics
Computer graphics deals with generating images with the aid of computers. Today, computer graphics is a core technology in digital photography, film, video games, cell phone and computer displays, and many specialized applications. A great deal ...
.[ 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 list
In computer science, a doubly linked list is a linked data structure that consists of a set of sequentially linked records called nodes. Each node contains three fields: two link fields (references to the previous and to the next node in the se ...
s 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 lib ...
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, a 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 a ...
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
In mathematics, particularly in order theory, an upper bound or majorant of a subset of some preordered set is an element of that is greater than or equal to every element of .
Dually, a lower bound or minorant of is defined to be an ele ...
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
Dijkstra's algorithm ( ) is an algorithm for finding the shortest paths between nodes in a graph, which may represent, for example, road networks. It was conceived by computer scientist Edsger W. Dijkstra in 1956 and published three years ...
, the minimum priorities form a monotonic sequence
In mathematics, a monotonic function (or monotone function) is a function between ordered sets that preserves or reverses the given order. This concept first arose in calculus, and was later generalized to the more abstract setting of order ...
, 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: