Fast and Simple Sorting Using Partial Information
Fast and Simple Sorting Using Partial Information
Abstract
We consider the problem of sorting a set of items having an unknown total order by doing binary
comparisons of the items, given the outcomes of some pre-existing comparisons. We present a simple
algorithm with a running time of O(m + n + log T ), where n, m, and T are the number of items, the
number of pre-existing comparisons, and the number of total orders consistent with the outcomes of the
pre-existing comparisons, respectively. The algorithm does O(log T ) comparisons.
Our running time and comparison bounds are best possible up to constant factors, thus resolving a
problem that has been studied intensely since 1976 (Fredman, Theoretical Computer Science). The best
previous algorithm with a bound of O(log T ) on the number of comparisons has a time bound of O(n2.5 )
and is significantly more complicated. Our algorithm combines three classic algorithms: topological sort,
heapsort with the right kind of heap, and efficient insertion into a sorted list.
1 Introduction
We consider the problem of sorting a set of items that are elements of a totally ordered set, assuming we are
given for free the outcomes of certain comparisons between the items. This problem has been called sorting
under partial information [Fre76; KK92], and it has been intensely studied since 1978.
We present a simple algorithm for this problem that runs in O(m + n + log T ) time and does O(log T )
comparisons, where n, m, and T are the number of items, the number of pre-existing comparisons, and the
number of total orders consistent with the pre-existing comparisons, respectively. These bounds are tight.
The best previous algorithm with an O(log T ) comparison bound has a time bound of O(n2.5 ) and is much
more complicated [Car+10].
Our algorithm for this problem combines three classic algorithms in a natural way: topological sort, heapsort
with the right kind of heap, and efficient insertion into a sorted list. Unlike previous algorithms, ours does
not use an estimate of T to determine the next comparison.
Our algorithm is closely related to a recent result of Haeupler, Hladı́k, Rozhoň, Tarjan, and Tětek [Hae+23]
proving that Dijkstra’s algorithm with an appropriate heap is universally optimal for the task of sorting
vertices according to their distance from a source vertex. We use the same kind of heap, and we use similar
ideas to analyze our algorithm.
∗ Partiallyfunded by the European Union’s Horizon 2020 ERC grant 949272.
† Supported by the VILLUM Foundation grant 54451. The work was done while this author was visiting BARC at the
University of Copenhagen. The author would like to thank Rasmus Pagh for hosting him there.
‡ This work was supported by the Fonds de la Recherche Scientifique-FNRS.
§ Supported by the European Research Council (ERC) under the European Unions Horizon 2020 research and innovation
1
Graph-theoretical problem formulation We assume the input is given in the form of a directed graph
G whose vertices are the items, having an arc vw for each given comparison outcome v < w. The goal is to
compute the unknown total order of the vertices of G by doing additional binary comparisons of the vertices.
The parameters n and m denote the number of vertices and the number of arcs of G, respectively.
The problem has a solution if and only if G is acyclic; that is, G is a directed acyclic graph (DAG). Each
possible solution is a topological order of the DAG, a total order such that if vw is an arc, v < w. A lower
bound on the number of comparisons needed to solve the problem is log T ,1 where T is the number of possible
topological orders of G.
An equivalent view of the problem is that the given comparisons define a partial order, and the problem is
to find an unknown linear extension of this partial order by doing additional comparisons. We prefer the
DAG view, because the input DAG may not be transitively closed (if uv and vw are arcs then so is uw)
nor transitively reduced (if vw is an arc then there is no other path from v to w). We want a solution for
an arbitrary DAG, and we do not want to take the time to compute either its transitive reduction or its
transitive closure. From now on, we call the problem DAG sorting.
Roadmap In Section 2, we briefly discuss related work on DAG sorting, on heaps with the working-set
bound (a key ingredient in our algorithm), on related sorting and ordering problems, and on sampling and
counting topological orders. In Section 3, we present our basic algorithm, which we call topological heapsort.
We prove that it runs in O(m + n + log T ) time and does O(n + log T ) comparisons. We eliminate the
additive n term in the number of comparisons in Section 4 by adding an additional step to our algorithm,
producing an algorithm that we call topological heapsort with insertion. This algorithm is best possible to
within constant factors in both its running time and its number of comparisons.
2 Related Work
DAG sorting Fredman [Fre76] considered the generalization of the DAG sorting problem in which there is
an unknown total order selected from an arbitrary subset of the total orders of n elements, and the problem
is to find the unknown total order by doing binary comparisons of the elements. He showed that if there are
T possible total orders, log T + 2n binary comparisons suffice. His algorithm is highly inefficient, however.
For the special case of sorting a DAG, Fredman [Fre76] and Linial [Lin84] independently conjectured that
there always exists a balancing comparison: a comparison “x < y?” such that the fraction of the topological
orders of G for which the answer is “yes” lies between δ and 1 − δ for δ = 1/3. This is the 1/3–2/3 conjecture.
Kahn and Saks [KS84] showed that the claim is true for δ = 3/11, and the value of δ has since been improved
several times [KL91; BFT95; Bri99]. It follows immediately that there is an algorithm that always does at
most log 1−δ
1 T comparisons: Repeatedly find a balancing comparison.
Kahn and Kim [KK92] gave a polynomial-time algorithm for the DAG sorting problem that does O(log T )
comparisons. Their algorithm uses the ellipsoid method to find a good comparison. Cardinal, Fiorini, Joret,
Jungers, and Munro [Car+10] gave several algorithms for sorting a DAG that avoid the use of the ellipsoid
method. In particular, they devised an algorithm that runs in O(n2.5 ) time and does O(log T ) comparisons.
This is the fastest previous algorithm that sorts a DAG in O(log T ) comparisons. Another algorithm in
[Car+10] has the same time bound and does (1 + ϵ) log T + Oϵ (n) comparisons.
Dushkin and Milo [DM18] considered a variant of the problem in which only the k smallest elements need to
be sorted. They devised an O(n log k + m) time algorithm that does O(n log k) comparisons. For additional
related work, see the survey of Cardinal and Fiorini [CF13].
Heaps with the working-set bound Our algorithm substantially deviates from the techniques previously
used for DAG sorting. Our basic algorithm is a classical topological sorting algorithm modified to use a heap
to choose the next-smallest vertex. We prove that if the heap has the so-called working-set bound, then our
DAG sorting algorithm is efficient.
A heap is a data structure H storing a set of items, each having a key selected from a totally ordered set.
The heap is initially empty. For our purpose, heaps support two operations. The first is insert(H, x), which
1 Throughout this paper “log” without a base denotes the base-two logarithm.
2
inserts item x into heap H; x must not be in H already and must have a predefined key. The second is
delete-min(H), which deletes and returns an item in H with minimum key. Some heap implementations
support a third operation, decrease-key(H, x, k), which, given the location of an item x in heap H such that
x has key greater than k, replaces the key of x in heap H by k. Fibonacci heaps [Fre+86b] and other equally
efficient heap implementations support insert and decrease-key in O(1) amortized time and delete-min on an
n-item heap in O(log n) time.
In our application we need a more refined bound for delete-mins that depends on the sequence of operations
done on the heap. This is the working-set bound, defined as follows. Consider an item x that is inserted into
the heap at some time and later deleted. An item is in the working set of x if it is x or is inserted after x
but before x is deleted. The number of items in the working set of x can change over time as other items are
inserted and deleted. The working set size w(x) of x is the maximum size of its working set while x is in the
heap. The heap has the working set bound if each insertion takes O(1) time and each delete-min of an item
x takes O(1 + log w(x)) time. These bounds can be amortized. (If decrease-key is a supported operation, it
has no required time bound.) We call a heap that has the working-set bound a working-set heap. Although
it is not obvious, one can prove that a heap with the working set bound has amortized time bounds of O(1)
for insert and O(log n) for delete-min [Hae+24].
The working-set bound is related to a similar bound for binary search trees, one possessed by splay trees [ST85].
Iacono [Iac00] proved that pairing heaps [Fre+86a], a form of self-adjusting heap, have the working-set bound,
provided that the heap ends empty. Splay trees used as heaps in an appropriate way also have the working-
set bound, again provided that the heap ends empty [Hae+24]. Elmasry [Elm06] devised a heap with an
even better bound: The time for a delete-min of x is O(1 + log s(x)), where s(x) is the size of the working
set of x when x is deleted. One can obtain the same bound in a more straightforward way using a finger
search tree [Hae+24]. Haeupler, Hladı́k, Rozhoň, Tarjan, and Tětek [Hae+23] developed a heap with the
working-set bound that also supports decrease-key operations in O(1) amortized time.
See Kozma and Saranurak [KS18], Munro, Peng, Wild, and Zhang [Mun+19], Elmasry, Farzan, and Iacono
[EFI13], and Bose, Howat, and Morin [BHM13] for other input-sensitive time bounds for heaps.
Related sorting and ordering problems Our approach is strongly influenced by recent work [Hae+23]
on the distance-ordering problem. The input to this problem is a directed graph with non-negative arc
weights and a source vertex. The problem is to sort the vertices of the input graph by their distance from
the source. This problem can be solved in O(m + n log n) time and comparisons by running Dijkstra’s
algorithm using a Fibonacci heap [Fre+86b]. The bounds are best possible in the worst case. The authors
of [Hae+23] improve this worst-case result by giving a so-called universally optimal algorithm: They show
that if Dijkstra’s algorithm is implemented using a heap with the working set bound, then for any fixed
unweighted graph G with a given source vertex, the algorithm takes the minimum time needed (to within a
constant factor) to solve the problem on G with a worst-case choice of arc weights. Moreover, a variant of
Dijkstra’s algorithm minimizes not only the time but also the number of comparisons to within a constant
factor.
The DAG sorting problem also asks for a universally optimal algorithm in that on any given DAG it should
minimize the running time and number of comparisons. Both problems are generalizations of sorting. Not
only are the two problems similar, but we show that they can be solved by similar techniques.
Another algorithm related to ours is the adaptive heapsort algorithm of Levcopoulos and Petersson [LP93].
This algorithm sorts a sequence of numbers by first building a heap-ordered tree, the Cartesian tree of the
sequence, defined recursively as follows: The root is the minimum number in the sequence, say x, its left
subtree is the Cartesian tree of the subsequence preceding x, and its right subtree is the Cartesian tree of
the subsequence following x. The Cartesian tree can be built in at most 2n − 3 comparisons. The algorithm
finishes the sort using a heap to store the possible minima, initially only the root. After a delete-min, say of
v, the children of v in the Cartesian tree are inserted in the heap. Levcopoulos and Petersson [LP93] use a
standard heap in their algorithm, but if one uses a working-set heap instead, our results imply that adaptive
heapsort does O(n + log T ) comparisons, where T is the number of topological orders of the Cartesian tree
viewed as a DAG.
An ordering problem that is dual to DAG sorting is that of producing a given partial order on a totally
ordered set, by doing enough comparisons so that the partial order induced by the comparison outcomes is
isomorphic to the input partial order. The two problems are dual in the sense that if an n-element partial
3
order has T topological orders, the partial-order production problem on the same partial order requires
log n! − log T comparisons [Sch76; Yao89], both worst-case and expected-case. Cardinal et al. [Car+09] gave
an algorithm for partial-order production that does a number of comparisons within a lower-order term of
the lower bound plus O(n). One step in their algorithm finds a greedy coloring of the comparability graph
of the partially ordered set. We use a similar concept, that of a greedy clique partition of an interval graph
(see Section 3.2), but only in our analysis, not in our algorithm, and our partition is of a different graph, one
determined by the behavior of our algorithm.
Sampling and counting topological orders Even though we give an algorithm that does O(log T )
comparisons, calculating log T exactly is hard, since the problem of determining T is #P-complete [BW91].
There are algorithms that compute T approximately [DFK91; Ban+10], however. Brightwell and Winkler
[BW91] have shown that a constant-factor approximation to T can be obtained with high probability using
O(n2 poly log n) calls to an oracle that samples a uniformly random topological order. There are multiple
results on sampling a random topological order [Mat91; KK91; BD99; Hub06], with the state-of-the-art
approach having O(n3 log n) time complexity and leading to an O(n5 poly log n) approximation algorithm
for T . In Appendix A, we show how to use one oracle call to get a constant-factor approximation to log T ,
and hence a polynomial approximation to T .
4
Algorithm 1: Topological heapsort
Data: A Directed Acyclic Graph G
Result: A sorted list of vertices Q
Initialize Q as an empty list to store sorted vertices
Compute the in-degree of each vertex
Initialize heap H to contain all vertices with in-degree zero (the sources)
while G is not empty :
Remove the minimum vertex v from H
Add v to the back of Q
foreach arc vw do
Decrement the in-degree of w
if w now has in-degree zero (is a source) :
Add w to H
Delete v and each outgoing arc vw from G
end
Topological heapsort is correct because if v is an undeleted vertex that is not a source, it cannot be the smallest
undeleted vertex. The running time of the algorithm is O(m + n) plus the time required for n insertions into
H and n intermixed delete-mins from H. All the vertex comparisons are in the heap operations. The input
can be a list of the arcs in G; if it is, as part of the initialization we build for each vertex a list of its outgoing
arcs.
We implement topological heapsort using a working-set heap.
Our approach is very similar to that used in [Hae+23]. We prove the theorem by developing a lower bound
on log T that can be related to the time required by the delete-min operations in topological heapsort via
the working-set bound.
Given a run of topological heapsort, let t(v) and t′ (v) be the times at which vertex v is inserted into and
deleted from the heap H, respectively. We associate the time interval I(v) = [t(v), t′ (v)] with vertex v. We
define the interval graph I of the run to be the undirected graph whose vertices are the vertices of the DAG
G, with an edge connecting two vertices if their intervals overlap. A clique of I is a set of pairwise adjacent
vertices. For any clique C of I, there is at least one time that is common to all of the corresponding intervals,
namely max{ t(v) | v ∈ C }, the insertion time of the last-inserted vertex in the clique. We call this time the
critical time of C.
We partition the vertices of I into cliques in a greedy fashion, by selecting any clique of maximum size,
deleting its vertices and incident edges, and repeating until there are no vertices left. Since the vertices of I
and G are the same, this also partitions the vertices of G. Although the first clique deleted is maximal in I
(no additional vertices can be added), this is not true of later cliques, since it may be possible to add to them
vertices that were deleted earlier. Let C1 , C2 , C3 , . . . , Ck be the sequence of cliques, in increasing order by
5
critical time. This order is in general different from their order of selection. We denote by |Ci | the number
of vertices in Ci .
If vw is an arc of G, then v must be deleted from H before w is inserted. Thus if v ∈ Ci and w ∈ Cj , then
i < j. In particular, no arcs of G have both ends in the same Ci . This allows us to prove the following
lemma:
Pk
Lemma 3.2. log T ≥ i=1 |Ci | log |Ci | − n log e.
Lemma 3.2 gives us a lower bound on log T . We need to relate this lower bound to the time taken by the
delete-min operations. We need one more definition. If C is any clique of I (not just a Ci ), the primary
vertex of C is the vertex in C inserted into H the earliest. Using this definition, we can reformulate the
definition of working-set size given in Section 2 as follows: The working-set size w(v) of a vertex v is the
maximum number of vertices in a clique of I whose primary vertex is v. This definition coincides with the
original definition for the original I, but w(v) can decrease as vertices are deleted from I.
The next lemma is the heart of our analysis. It is a variant of Lemmas 3.14 and 3.15 in [Hae+23].
P Pk
Lemma 3.3. v∈I log w(v) ≤ 2 i=1 |Ci | log |Ci |, where w(v) is the working-set size of v in I.
Proof. We prove the lemma by induction on k. Suppose the lemma is true for k − 1. Let C be the first clique
selected when building the clique partition, let I ′ be I after the vertices in C and their incident edges are
deleted, and let Cj′ for 1 ≤ j ≤ k − 1 be a renumbering of the cliques Ci , not including C. (The order does
Pk−1
not matter.) By the induction hypothesis, v∈I ′ w′ (v) ≤ 2 i=1 |Ci′ | log |Ci′ |, where w′ (v) is the working-set
P
size of v in I ′ .
To prove the bound for I, we must account for the terms for the clique C and its vertices, and also for any
increases in the working-set sizes of vertices in I ′ caused by adding the vertices in C. The first is easy, the
second is the technical part of the proof.
P
Since C is a largest clique in I, w(v) ≤ |C| for every vertex v in C. Hence v∈C log w(v) ≤ |C| log |C|.
Consider the increase in working-set sizes that results from adding one vertex v in C to I ′ . This can increase
the working-set size of u ∈ I ′ only if t(u) < t(v) < t′ (u), and then by at most one. Let u1 , u2 , . . . , uj be
the vertices u ∈ I ′ such that t(u) < t(v) < t′ (u), ordered in decreasing order by t(ui ). Then the increase in
log w(ui ) caused by adding v is at most log(i + 1) − log i by the concavity of the log function. Summing over
i, the sum telescopes, and we find that the total increase is at most log(j + 1). The set containing v and all
the vertices ui is a clique in I. By the greedy choice of C, j + 1 ≤ |C|. This argument applies sequentially
to each addition of a vertex v in C to I ′ . Thus u∈I ′ (w(u) − w′ (u)) ≤ |C| log |C|.
P
Adding our two bounds and the bound given by the induction hypothesis gives us the desired bound for I.
The lemma follows by induction.
Theorem 3.1 follows immediately from Lemmas 3.2 and 3.3, since the running time of topological heapsort is
O(m + n) plus the time to do the heap operations, the time to do the heap operations is O(n + log T ) by the
two lemmas and the definition of the working-set bound, and all the comparisons are in the heap operations.
Lemma 4.1. Let ℓ be the number of vertices on a longest path in a DAG G. Then G has at least 2(n−ℓ)/2
topological orders.
6
G P
Marked vertices
are denoted by
G′
Figure 1: This picture shows how the input DAG G is transformed into a DAG G′ in Step 2 of topological
heapsort with insertion.
Proof. Consider the partition of the vertices of G into layers L1 , L2 , L3 , . . . , Lℓ , where layer Li contains all
vertices v such that the longest path ending in v contains i vertices. Then if vw is an arc with v ∈ Li and
w ∈ Lj , then i < j. In particular, there are no arcs with both ends in the same layer. It follows as in the
Qℓ
proof of Lemma 3.2 that the number of topological orders of G is at least i=1 |Li |!: The vertices can be
ordered layer by layer, and those within a layer can be ordered arbitrarily. A layer that contains 2k or 2k + 1
vertices contributes at least (2k)! ≥ 2k to this product. This means that each layer Li contributes to the
product at least 2(|Li |−1)/2 . The number of topological orders is thus at least
ℓ
Y Pℓ
2(|Li |−1)/2 = 2 i=1 (|Li |−1)/2 = 2(n−ℓ)/2
i=1
By Lemma 4.1, topological heapsort uses the desired bound of O(log T ) comparisons to sort the vertices of
any DAG whose maximum-length path contains at most (1 − ϵ)n vertices, where ϵ is any positive constant,
since in this case log T = Ω(n). Thus we only need a way to handle any DAG that has a path containing
at least a large constant fraction of the vertices. To handle such a DAG, we find a longest path and run
topological heapsort without inserting any of the vertices on this path into the heap. Instead, we insert each
vertex returned by a delete-min into the long path using an exponential search followed by a binary search
on the relevant part of the path to find the insertion position.
Topological heapsort with insertion The resulting sorting algorithm, which we call topological heapsort
with insertion, is as follows:
Step 1: Find a longest path P in the given DAG G. Mark the last vertex of P . For each vertex v not
on P , mark the last vertex u on P such that uv is an arc and the first vertex w on P such that vw is an
arc. Delete all arcs between v and P other than uv and vw. Form DAG G′ from G by adding an arc
xy from each marked vertex x on P other than the last one to the next marked vertex y, and deleting
all unmarked vertices on P and their incident arcs. Save the vertices that were originally on P in an
array L.
Step 2: Run topological heapsort on G′ . Each time a vertex v is deleted from H, do the following:
Delete from L all vertices less than or equal to v, and add these vertices to the sorted list Q in their
order in L. Add v to Q as well. Then continue executing topological heapsort.
To do Step 1, find any topological order of the vertices of G and process the vertices in topological order
as follows: Compute for each vertex v the length ℓ(v) of the longest path ending in v, using the recurrence
ℓ(v) = max({0} ∪ { ℓ(u) + 1 | uv ∈ E }}, where E is the set of arcs of G. Find a longest path P by starting
at a vertex v of largest ℓ(v) and proceeding backward, always to a vertex u of largest ℓ(u). Finding a longest
path takes O(m + n) time and no vertex comparisons.
7
In Step 2, find the set of vertices in L less than or equal to v as follows: If v is in L, then this set is the prefix
of L ending with v. Testing this requires no vertex comparisons. If v is not in L, use exponential search
followed by binary search: Compare v with the first, second, fourth, eighth, . . . vertex in L until finding a
pair of vertices y and z in L such that y < v < z (possibly y = ∅ if v is less than the first vertex in L). Then
do a binary search on the set of vertices in L between y and z to find x. A search that returns the k-th vertex
on P takes O(log k) time and O(log k) comparisons.
The running time of topological heapsort with insertion is O(m + n + log T ). We shall show that the number
of comparisons it does is O(log T ). We need the following simple monotonicity lemma:
Lemma 4.2. Let G be a DAG. Suppose that a DAG G′ is formed by (i) deleting an arc vw such that there
is a path from v to w avoiding this arc, (ii) adding an arc to G, (iii) deleting a vertex v with one incoming
arc uv and one outgoing arc vw and adding an arc uw, or (iv) removing a source vertex. Then G has at
least as many topological orders as G′ .
Proof. (i) If G′ is formed by deleting an arc vw such that there is a path from v to w avoiding vw, then the
set of topological orders of G is the same as that of G′ . (ii) If G′ is formed from G by adding an arc, then
any topological order of G′ is a topological order of G. (iii) If G′ is formed from G by deleting a vertex v
with incident arcs uv and vw and adding uw, then any topological order of G′ can be extended to one of G
by inserting v anywhere between u and w. (iv) Any topological order of G′ may be extended to a topological
order of G by inserting v before all the vertices of G′ .
Theorem 4.3. Topological heapsort with insertion sorts the vertices of a DAG G in O(m + n + log T ) time
and O(log T ) comparisons.
Proof. The time that the algorithm spends outside of Algorithm 1 is O(n + m). Hence the running time is
O(m + n + log T ) by Theorem 3.1. It remains to prove that the algorithm does O(log T ) comparisons.
Let ℓ be the number of vertices on P . By Lemma 4.1, (n − ℓ)/2 ≤ log T . The number of vertices in G′ is at
most 3(n−ℓ), so the number of comparisons needed by topological sort when run on G′ is O((n−ℓ)+log T ) =
O(log T ) by Theorem 3.1 and Lemma 4.2.
Consider the search that Step 2 does for a vertex v not in P . We use P (v) to denote the set of vertices removed
from P when processing v in Step 2. The number of comparisons the search takes is O(1 + log(|P (v)| + 1)).
For any correct order of the vertices of G and any node v, consider the |P (v)| + 1 topological orders obtained
by moving v by 0, 1, . . ., |P (v)| positions to the left in the current topological order. Note that these are
indeed valid topological orders, since the |P (v)| vertices of G immediately preceding v have to be part of
P byQthe definition of P (v). We may shift v in this fashion P independently for every vertex v. Therefore,
T ≥ v∈V (G′ ) (|P (v)| + 1). We can rewrite this inequality as v∈V (G′ ) log(|P (v)| + 1) ≤ log T ; and, finally,
P
conclude that v∈V (G′ ) (1 + log(|P (v)| + 1)) ≤ log T + (n − ℓ) = O(log T ). Thus our algorithm does at most
O(log T ) comparisons during Step 2.
Combining the bounds for running topological heapsort on G′ and inserting vertices into P gives the theorem.
Remark. If Step 1 finds that the longest path has length at most (1 − ϵ)n for some fixed positive ϵ, the
algorithm can skip the construction of G′ and just run topological heapsort on the original graph G. Also,
if the problem must be solved repeatedly for a fixed DAG with different total orders, Step 1 needs to be run
only once.
References
[Ban+10] Jacqueline Banks, Scott Garrabrant, Mark L Huber, and Anne Perizzolo. “Using TPA to count
linear extensions”. In: arXiv preprint arXiv:1010.4981 (2010).
[BHM13] Prosenjit Bose, John Howat, and Pat Morin. “A history of distribution-sensitive data structures”.
In: Space-Efficient Data Structures, Streams, and Algorithms: Papers in Honor of J. Ian Munro
on the Occasion of His 66th Birthday (2013), pp. 133–149.
[Bri99] Graham Brightwell. “Balanced pairs in partial orders”. In: Discrete Mathematics 201.1-3 (1999),
pp. 25–52.
8
[BW91] Graham Brightwell and Peter Winkler. “Counting linear extensions is #P-complete”. In: Pro-
ceedings of the twenty-third annual ACM symposium on Theory of computing. 1991, pp. 175–
181.
[BFT95] Graham R Brightwell, Stefan Felsner, and William T Trotter. “Balancing pairs and the cross
product conjecture”. In: Order 12 (1995), pp. 327–349.
[BD99] Russ Bubley and Martin Dyer. “Faster random generation of linear extensions”. In: Discrete
mathematics 201.1-3 (1999), pp. 81–88.
[CF13] Jean Cardinal and Samuel Fiorini. “On generalized comparison-based sorting problems”. In:
Space-Efficient Data Structures, Streams, and Algorithms: Papers in Honor of J. Ian Munro on
the Occasion of His 66th Birthday. Springer, 2013, pp. 164–175.
[Car+10] Jean Cardinal, Samuel Fiorini, Gwenaël Joret, Raphaël M Jungers, and J. Ian Munro. “Sorting
under partial information (without the ellipsoid algorithm)”. In: Proceedings of the forty-second
ACM symposium on Theory of computing. 2010, pp. 359–368.
[Car+09] Jean Cardinal, Samuel Fiorini, Gwenaël Joret, Raphaël M. Jungers, and J. Ian Munro. “An
efficient algorithm for partial order production”. In: Proceedings of the 41st Annual ACM Sym-
posium on Theory of Computing, STOC 2009, Bethesda, MD, USA, May 31 - June 2, 2009.
Ed. by Michael Mitzenmacher. ACM, 2009, pp. 93–100.
[DM18] Eyal Dushkin and Tova Milo. “Top-k sorting under partial order information”. In: Proceedings
of the 2018 International Conference on Management of Data. 2018, pp. 1007–1019.
[DFK91] Martin Dyer, Alan Frieze, and Ravi Kannan. “A random polynomial-time algorithm for approx-
imating the volume of convex bodies”. In: Journal of the ACM (JACM) 38.1 (1991), pp. 1–
17.
[Elm06] Amr Elmasry. “A priority queue with the working-set property”. In: International Journal of
Foundations of Computer Science 17.06 (2006), pp. 1455–1465.
[EFI13] Amr Elmasry, Arash Farzan, and John Iacono. “On the hierarchy of distribution-sensitive prop-
erties for data structures”. In: Acta informatica 50.4 (2013), pp. 289–295.
[Flo64] Robert W Floyd. “Algorithm 245: treesort”. In: Communications of the ACM 7.12 (1964), p. 701.
[Fre76] Michael L Fredman. “How good is the information theory bound in sorting?” In: Theoretical
Computer Science 1.4 (1976), pp. 355–361.
[Fre+86a] Michael L Fredman, Robert Sedgewick, Daniel D Sleator, and Robert E Tarjan. “The pairing
heap: A new form of self-adjusting heap”. In: Algorithmica 1.1-4 (1986), pp. 111–129.
[Fre+86b] Michael L. Fredman, Robert Sedgewick, Daniel Dominic Sleator, and Robert Endre Tarjan. “The
Pairing Heap: A New Form of Self-Adjusting Heap”. In: Algorithmica 1.1 (1986), pp. 111–129.
doi: 10.1007/BF01840439. url: https://doi.org/10.1007/BF01840439.
[Hae+24] Bernhard Haeupler, Richard Hladı́k, John Iacono, Václav Rozhoň, Robert E. Tarjan, and Jakub
Tětek. Heaps with the Working-Set Bound. Preprint. 2024.
[Hae+23] Bernhard Haeupler, Richard Hladı́k, Václav Rozhoň, Robert Tarjan, and Jakub Tětek. Universal
Optimality of Dijkstra via Beyond-Worst-Case Heaps. 2023. arXiv: 2311.11793 [cs.DS].
[Hub06] Mark Huber. “Fast perfect sampling from linear extensions”. In: Discrete Mathematics 306.4
(2006), pp. 420–428.
[Iac00] John Iacono. “Improved upper bounds for pairing heaps”. In: Scandinavian Workshop on Algo-
rithm Theory. Springer. 2000, pp. 32–45.
[Kah62] Arthur B Kahn. “Topological sorting of large networks”. In: Communications of the ACM 5.11
(1962), pp. 558–562.
[KK92] Jeff Kahn and Jeong Han Kim. “Entropy and sorting”. In: Proceedings of the twenty-fourth
annual ACM symposium on Theory of computing. 1992, pp. 178–187.
[KL91] Jeff Kahn and Nathan Linial. “Balancing extensions via Brunn-Minkowski”. In: Combinatorica
11.4 (1991), pp. 363–368.
[KS84] Jeff Kahn and Michael Saks. “Balancing poset extensions”. In: Order 1 (1984), pp. 113–126.
9
[KK91] Alexander Karzanov and Leonid Khachiyan. “On the conductance of order Markov chains”. In:
Order 8 (1991), pp. 7–15.
[Knu97] Donald E Knuth. The Art of Computer Programming: Fundamental Algorithms, volume 1.
Addison-Wesley Professional, 1997.
[KS18] László Kozma and Thatchaphol Saranurak. “Smooth heaps and a dual view of self-adjusting
data structures”. In: Proceedings of the 50th Annual ACM SIGACT Symposium on Theory of
Computing. 2018, pp. 801–814.
[LP93] Christos Levcopoulos and Ola Petersson. “Adaptive heapsort”. In: Journal of Algorithms 14.3
(1993), pp. 395–413.
[Lin84] Nathan Linial. “The information-theoretic bound is good for merging”. In: SIAM Journal on
Computing 13.4 (1984), pp. 795–801.
[Mat91] Peter Matthews. “Generating a random linear extension of a partial order”. In: The Annals of
Probability 19.3 (1991), pp. 1367–1392.
[Mun+19] J Ian Munro, Richard Peng, Sebastian Wild, and Lingyi Zhang. “Dynamic Optimality Refuted–
For Tournament Heaps”. In: arXiv preprint arXiv:1908.00563 (2019).
[Sch76] A. Schönhage. “The Production of Partial Orders”. In: Astérisque 38-39 (1976), pp. 229–246.
[Sha48] Claude Elwood Shannon. “A mathematical theory of communication”. In: The Bell system tech-
nical journal 27.3 (1948), pp. 379–423.
[ST85] Daniel Dominic Sleator and Robert Endre Tarjan. “Self-adjusting binary search trees”. In: Jour-
nal of the ACM (JACM) 32.3 (1985), pp. 652–686.
[Wil64] J Williams. “Heapsort”. In: Commun. ACM 7.6 (1964), pp. 347–348.
[Yao89] Andrew Chi-Chih Yao. “On the Complexity of Partial Order Productions”. In: SIAM J. Comput.
18.4 (1989), pp. 679–689.
Proof. The algorithm merely samples a topological order, runs topological heapsort with insertion on the
sample, and returns the number of comparisons made by the algorithm. The running time of the algorithm
is O(n + m + log T + tsample ). Since in order to sample the order, we have to read the whole input DAG, this
is equal to the desired O(tsample + log T ). The number of comparisons it does is O(log T ) (note that we do
not use any comparisons when generating the sample).
The number of comparisons X done by topological heapsort with insertion is X = O(log T ) by Theorem 4.3.
Thus our algorithm returns an upper bound on log T that is at most a constant factor larger than the true
value.
It remains to give a similar lower bound. We prove that with high probability X = Ω(log T ). Suppose O is a
sample c-pointwise close to uniform. Consider the event E that X ≤ log(T10
/c)
. For any topological order O′ ,
we have
P [O = O′ ∩ E] P [O = O′ ] c/T
P [O = O′ |E] = ≤ ≤ .
P [E] P [E] P [E]
2 We say that a distribution p is c-pointwise close to q if for every element x we have qx /c ≤ px ≤ c · qx .
10
The conditional entropy of O is then
X
′ 1 T · P [E]
H(O|E) = P [O = O |E] · log ≥ log .
P [O = O′ |E] c
O ′ ∈E
The comparisons performed by topological heapsort with insertion uniquely determine the topological order
O. Thus by Shannon’s source coding theorem for symbol codes [Sha48], we have E[X|E] ≥ H(O|E), and we
can write
log(T /c) T · P [E]
≥ E[X|E] ≥ H(O|E) ≥ log .
10 c
Solving for P [E], we get
c 9/10
P [E] ≤ ,
T
which concludes the proof.
11