Naive implementations One can create a simple, but inefficient priority queue in a number of ways. These naive implementations can demonstrate the expected behaviour of a priority queue in a simpler manner. •
insert elements into an unsorted array; find and
extract element with
highest priority • : Performance: "insert" performs in O(1) constant time, where "extract_max" performs in O(n) linear time.
insert(element, priority): node.element ← element node.priority ← priority list.append(node)
extract_max(): highest ← 0 foreach node in list: if highest.priority insert" performs in O(n) linear time, where "extract_max" performs in O(1) constant time.
insert(element, priority): node.element ← element node.priority ← priority for i in [0...N]: element ← list.get_at_index(i) if element.priority O(\log n) performance for inserts and removals, and O(n) to build the
heap initially from a set of n elements. Variants of the basic heap data structure such as
pairing heaps or
Fibonacci heaps can provide better bounds for some operations. Alternatively, when a
self-balancing binary search tree is used, insertion and removal also take O(\log n) time, although building trees from existing sequences of elements takes O(n \log n) time; this is typical where one might already have access to these data structures, such as with third-party or standard libraries. From a space-complexity standpoint, using
self-balancing binary search tree with
linked list takes more storage, since it requires to store extra references to other nodes. From a computational-complexity standpoint, priority queues are congruent to sorting algorithms. The section on
the equivalence of priority queues and sorting algorithms, below, describes how efficient sorting algorithms can create efficient priority queues.
Specialized heaps There are several specialized
heap data structures that either supply additional operations or outperform heap-based implementations for specific types of keys, specifically integer keys. Suppose the set of possible keys is \{1, 2, ..., C\}. • When only insert, find-min and extract-min are needed and in case of integer priorities, a
bucket queue can be constructed as an array of C
linked lists plus a pointer \text{top}, initially C. Inserting an item with key k appends the item to the k'th list, and updates \text{top} \gets \text{min}(\text{top}, k), both in constant time. extract-min deletes and returns one item from the list with index \text{top}, then increments \text{top} if needed until it again points to a non-empty list; this takes O(C) time in the worst case. These queues are useful for sorting the vertices of a graph by their degree. • A
van Emde Boas tree supports the minimum, maximum, insert, delete, search, extract-min, extract-max, predecessor and successor] operations in O(\log \log C) time, but has a space cost for small queues of about O(2^{m/2}), where m is the number of bits in the priority value. The space can be reduced significantly with hashing. • The
Fusion tree by
Fredman and
Willard implements the minimum operation in O(1) time and insert and extract-min operations in O(\log n / \log \log C)time. However it is stated by the author that, "Our algorithms have theoretical interest only; The constant factors involved in the execution times preclude practicality." For applications that do many "
peek" operations for every "extract-min" operation, the time complexity for peek actions can be reduced to O(1) in all tree and heap implementations by caching the highest priority element after every insertion and removal. For insertion, this adds at most a constant cost, since the newly inserted element is compared only to the previously cached minimum element. For deletion, this at most adds an additional "peek" cost, which is typically cheaper than the deletion cost, so overall time complexity is not significantly impacted.
Monotone priority queues are specialized queues that are optimized for the case where no item is ever inserted that has a lower priority (in the case of min-heap) than any item previously extracted. This restriction is met by several practical applications of priority queues.
Summary of running times == Equivalence of priority queues and sorting algorithms ==