Melding priority queues ∗ Ran Mendelson † Robert E. Tarjan ‡ Mikkel Thorup § Uri Zwick † June 11, 2004 Abstract We show that any priority queue data structure that supports insert, delete, and find-min operations in pq(n) time, when n is an upper bound on the number of elements in the priority queue, can be converted into a priority queue data structure that also supports fast meld operations with essentially no increase in the amortized cost of the other operations. More specifically, the new data structure supports insert, meld and find-min operations in O(1) amortized time, and delete operations in O(pq(n)+ α(n, n)) amortized time, where α(m, n) is a functional inverse of the Ackermann function. The construction is very simple, essentially just placing a non-meldable priority queue at each node of a union-find data structure. We also show that when all keys are integers in the range [1,N ], we can replace n in the bound stated above by min{n, N }. Applying this result to non-meldable priority queue data structures obtained recently by Thorup, and by Han and Thorup, we obtain meldable RAM priority queues with O(log log n) amortized cost per operation, or O( √ log log n) expected amortized cost per operation, respectively. As a by-product, we obtain improved algorithms for the minimum directed spanning tree problem in graphs with integer edge weights: A deterministic O(m log log n) time algorithm and a randomized O(m √ log log n) time algorithm. These bounds improve, for sparse enough graphs, on the O(m + n log n) running time of an algorithm by Gabow, Galil, Spencer and Tarjan that works for arbitrary edge weights. Key Words: Priority queues, heaps, union-find, word RAM model, optimum branchings, minimum directed spanning trees. 1 Introduction Priority queues are basic data structures used by many algorithms. The most basic operations, supported by all priority queues, are insert, which inserts an element with an associated key into the priority queue, and extract-min, which returns the element with the smallest key currently in the queue, and deletes it. These two operations can be used, for example, to sort n elements by performing n insert operations followed by n extract-min operations. Most priority queues also support a delete operation, that deletes a given element, not necessarily with the minimum key, from the queue, and find-min, which finds, but does not delete, an element with minimum key. Using the insert and delete operations we can easily implement a decrease-key operation, or more generally a change-key operation, that decreases, or arbitrarily changes, the key of a queue element. (We simply delete the element from the queue and re-insert it with its new key.) As the decrease-key operation is the bottleneck operation in efficient implementations of Dijkstra’s single-source shortest paths algorithm [9], and Prim’s algorithm [25] for finding a minimum spanning tree, many priority queues support this operation directly, sometimes in constant time. The efficient implementation of several algorithms, such as the algorithm of Edmonds [10] for computing optimum branching and minimum directed spanning trees, require the maintenance of a collection of priority queues. In * This paper is the combined journal version of [23] and [22]. † School of Computer Science, Tel-Aviv University, Tel-Aviv, 69978, Israel. E-Mail: {ranm,zwick}@cs.tau.ac.il. ‡ Department of Computer Science, Princeton University, Princeton, NJ 08540, USA and Hewlett Packard, Palo Alto, CA 94304, USA. Research at Princeton University partially supported by the Aladdin project, NSF Grant CCR-9626862. E-mail: ret@cs.princeton.edu. § AT&T Labs - Research, 180 Park Avenue, Florham Park, NJ 07932, USA. E-mail: mthorup@research.att.com. 1