arXiv:2611.06745cs.DSNov 2026

Manuscript Offprint

arXiv:2611.06745

Generalized Sorting via Four-Cycles

Soheil Behnezhad, Shane Ferrante

Abstract

We study the generalized sorting problem where only a subset of the pairs can be compared. Equivalently, we are given an nn-vertex mm-edge graph G=(V,E)G = (V, E) where the vertex set VV is to be sorted, and the edge set EE indicates possible comparisons. The goal is to sort VV with as few (adaptive) queries to EE as possible.

When GG is the complete graph, the problem is equivalent to standard sorting for which Θ(nlog⁡n)\Theta(n \log n) queries are sufficient and necessary. All classical sorting algorithms crucially rely on the ability to compare any pair and do not imply an o(n2)o(n^2)-query algorithm in the general case. This was improved 15 years ago by Huang, Kannan, and Khanna [FOCS'11] who achieved a bound of O~(nn)\tilde{O}(n\sqrt{n}). Kuszmaul and Narayanan [FOCS'21] later gave a bound of O~(mn)\tilde{O}(\sqrt{mn}), which is more efficient for sparse graphs, but matches the O~(nn)\tilde{O}(n\sqrt{n}) barrier for dense graphs.

In this paper, we present an algorithm for generalized sorting that makes at most O~(n4/3)\tilde{O}(n^{4/3}) queries, therefore breaking the longstanding O~(nn)\tilde{O}(n\sqrt{n}) barrier for all graphs. The key to our result is to query edges that belong to many (specific types of) four-cycles. We show that querying such edges allows the algorithm to verify the direction of many other edges (specifically, their opposing edges in the four-cycles) and thus achieve a much better amortized query cost.

*Supported in part by NSF CAREER Award CCF 2442812, a Sloan Fellowship, and a Google Research Award.

1 Introduction

The standard sorting problem assumes that any pair of elements can be compared. A well-studied generalization, known as the generalized sorting problem [HKK11], removes this freedom. We are given a simple undirected graph G=(V,E)G = (V, E) on nn vertices and mm edges. The vertices have an unknown total order ≺\prec, and a query on {u,v}∈E\{u, v\} \in E reveals whether u≺vu \prec v or v≺uv \prec u. Queries outside EE are forbidden. We are promised that consecutive vertices in the true order are joined by an edge. Equivalently, directing every allowed edge according to ≺\prec produces an acyclic graph with a directed Hamiltonian path. The task is to recover this order using few queries.

Background: If GG is the complete graph, then the problem is equivalent to classical comparison-based sorting for which Θ(nlog⁡n)\Theta(n \log n) queries are sufficient and necessary. Another well-studied special case of the generalized sorting problem is when GG is the complete bipartite graph, which is known as the nuts-and-bolts matching problem. For this variant, too, a long line of work eventually achieved the optimal query complexity of Θ(nlog⁡n)\Theta(n \log n) [Raw92; Alo+94; BF95; Bra95; ABF96; KMS98].

The situation is much less understood for general graphs. The trivial algorithm is to use up to O(n2)O(n^2) queries to learn the direction of every edge in the graph to discover the directed Hamiltonian path. Huang, Kannan, and Khanna [HKK11] were the first to break this bound. They proved 15 years ago that there is a randomized generalized sorting algorithm with query complexity O~(nn)\widetilde{O}(n\sqrt{n}). Roughly speaking, their algorithm iteratively discovers ordered sets of length Θ(n)\Theta(\sqrt{n}) using O~(n)\widetilde{O}(n) queries. Repeating this O(n)O(\sqrt{n}) times achieves the claimed O~(nn)\widetilde{O}(n\sqrt{n}) query complexity. A more recent algorithm by Kuszmaul and Narayanan [KN21]—building on the work of Lu et al. [Lu+21]—achieves a bound of O~(mn)\widetilde{O}(\sqrt{mn}). Their algorithm keeps querying the edges of GG uniformly at random until these queries become very unlikely to discover “inversions”, at which point they switch to the prediction-based algorithm of [Lu+21].

The algorithms of [HKK11] and [KN21] achieve the same bound of O~(nn)\widetilde{O}(n\sqrt{n}) for general graphs using very different approaches. This is also a natural bound for various other graph problems. For example, Θ~(nn)\widetilde{\Theta}(n\sqrt{n}) is provably the right bound for (Δ+1)(\Delta + 1) vertex coloring under neighbor and pair queries [ACK19], and for one-sided testing of graph isomorphism under adjacency-matrix queries [FM08]. The same bound is optimal in quantum query complexity, for problems such as graph connectivity, minimum spanning tree [Dür+06], and testing graph planarity or acyclicity [CK12], among many others. It is therefore natural to ask:

Is O~(nn)\widetilde{O}(n\sqrt{n}) the right bound for generalized sorting?

Our result: In this paper, we break through the longstanding O~(nn)\widetilde{O}(n\sqrt{n}) barrier for generalized sorting, showing that it is indeed not the right bound. More specifically, we prove that:

Theorem 1. Generalized sorting on an nn-vertex graph admits a randomized polynomial-time algorithm that succeeds with probability 1−1/n1 - 1/n and makes at most O(n4/3log⁡4/3n)O\left(n^{4/3} \log^{4/3} n\right) queries.

Our key ingredient to prove Theorem 1 is to query edges that belong to many (specific types of) length-four cycles that are abundant in dense graphs. We show that querying such edges allows the algorithm to verify the direction of many other edges (specifically, their opposing edges in the four-cycles) and thus achieve a much better amortized query cost. We give an overview of this idea and the overall proof in Section 2.

Further related work: Generalized sorting builds on earlier work on restricted comparisons and nonuniform comparison costs. Charikar et al. [Cha+00] first introduced the priced-information framework, evaluating query strategies against the cost of a cheapest certificate. Within this framework, Gupta and Kumar [GK01] studied sorting and selection with structured comparison costs, and Kannan and Khanna [KK03] studied selection with monotone comparison costs.

Another related line of work studies monotonicity testing, including whether an array of numbers is sorted or far from sorted, using few queries. Each query reveals an array entry, and the distance to sortedness is the minimum fraction of entries that must be changed to make the array nondecreasing. Sortedness testing, tolerant testing, and extensions to partially ordered domains have been studied extensively; see, e.g., [Erg+98; Fis+02; Fis04; Ail+04; PRR06; SS10; Ras16; Bel18; Ron25; Yos26] and the references therein.

References (31)