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 arrays 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 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.
Example For example, consider a bucket queue with four priorities, the numbers 0, 1, 2, and 3. It consists of an array A whose four cells each contain a collection of elements, initially empty. For the purposes of this example, A can be written as a bracketed sequence of four sets: A = [\emptyset, \emptyset, \emptyset, \emptyset]. Consider a sequence of operations in which we insert two elements x and y with the same priority 1, insert a third element z with priority 3, change the priority of x to 3, and then perform two extractions of the minimum-priority element. • After inserting x with priority 1, A = [\emptyset, \{x\}, \emptyset, \emptyset]. • After inserting y with priority 1, A = [\emptyset, \{x,y\}, \emptyset, \emptyset]. • After inserting z with priority 3, A = [\emptyset, \{x,y\}, \emptyset, \{z\}]. • Changing the priority of x from 1 to three involves removing it from A[1] and adding it to A[3], after which A = [\emptyset, \{y\}, \emptyset, \{x,z\}]. • Extracting the minimum-priority element, in the basic version of the bucket queue, searches from the start of A to find its first non-empty element: A[0] is empty but A[1] = \{y\}, a non-empty set. It chooses an arbitrary element of this set (the only element, y) as the minimum-priority element. Removing y from the structure leaves A = [\emptyset, \emptyset, \emptyset, \{x,z\}]. • The second extract operation, in the basic version of the bucket queue, searches again from the start of the array: A[0] = \emptyset, A[1] = \emptyset, A[2] = \emptyset, A[3] = {}non-empty. In the improved variants of the bucket queue, this search starts instead at the last position that was found to be non-empty, A[1]. In either case, A[3] = \{x,z\} is found to be the first non-empty set. One of its elements is chosen arbitrarily as the minimum-priority element; for example, z might be chosen. This element is removed, leaving A = [\emptyset, \emptyset, \emptyset, \{x\}]. ==Applications==